getaddrinfo memory leak

こ雲淡風輕ζ 提交于 2019-12-05 11:27:46

问题


I have this code for getting information about IPv4 address:

struct addrinfo hints, *info = NULL;
char addr4[INET_ADDRSTRLEN];

memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
hints.ai_family = AF_INET;

if (!getaddrinfo(argv[hostPara], NULL, &hints, &info)) {
    inet_ntop(AF_INET, &((const sockaddr_in *)info->ai_addr)->sin_addr, addr4, INET_ADDRSTRLEN);
}
if (info != NULL) {
    freeaddrinfo(info);
}

but if I tested argv[hostPara] is "www.google.com" I am getting this from valgrind:

==3632== 168 bytes in 1 blocks are still reachable in loss record 1 of 1
==3632==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3632==    by 0x524B5B8: make_request (check_pf.c:249)
==3632==    by 0x524BA53: __check_pf (check_pf.c:342)
==3632==    by 0x5201134: getaddrinfo (getaddrinfo.c:2458)
==3632==    by 0x40186B: main (trace.cc:214)

and if argv[hostPara] is "www.ubuntu.com" there are no memory leaks. What is this magic behaviour?


回答1:


Looking a bit the gblic, it's about object catching in case of ipv6 (look 249 line).

As other members have explained, "still reachable" is not an error itself, but it may hide some buggy situations. In this case it's not a problem, just a warning about something that could hide something nasty.

This warning has also been reported to redhat

The reason of the warning for google and not for ubuntu it's beacause google has ipv6 deployed on its servers and ubuntu not, and then the catching is not performed. You can check it with:

nslookup -q=AAAA www.google.com  and nslookup -q=AAAA www.ubuntu.com



回答2:


This may not be a memory leak (technically it is, but you shouldn't worry about it) sometimes libraries allocate memory the first time a function is called for subsequent calls. you can have valgrind suppress those errors if you want.

From the FAQ:

"still reachable" means your program is probably ok -- it didn't free some memory it could have. This is quite common and often reasonable. Don't use --show-reachable=yes if you don't want to see these reports.




回答3:


It says "still reachable". That probably means that the library allocated some memory for a cache or something like that and doesn't want to free it. You can safely ignore it or at least it needs more analysis than just saying it's a memory leak.

Why there's a difference between different hosts is anyones guess. Probably because different name servers require different type of work.



来源:https://stackoverflow.com/questions/13229913/getaddrinfo-memory-leak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!