17

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 when I tested when 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)

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

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Petr Přikryl
  • 1,641
  • 4
  • 22
  • 34
  • 2
    May be because google.com has switched over to ipv6 and ubuntu.com has not? I.e. with google you get buffer overrrun in addr4 –  Nov 05 '12 at 10:21
  • 1
    Does this sum up if you repeat calling `getaddrinfo()`/`freeaddrinfo()`? – alk Nov 05 '12 at 10:27
  • And there is a way how to check what versions of IP dns server useses? (without memory leaks) – Petr Přikryl Nov 05 '12 at 10:28
  • For debugging purpose: comment out the call to `inet_ntop()`, just add some calls to `getaddrinfo()`/`freeaddrinfo()` (try to query for the same host as well as to others, ip4 and ip6 ones) and compare the results to the bahaviour you initially observed. Are there any differences. If not the answers given so far, should be Ok. – alk Nov 05 '12 at 10:45
  • Consider using getnameinfo instead of inet_ntop. It has a NI_NUMERICHOST flag. – Per Johansson Jan 31 '13 at 22:17

3 Answers3

11

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
nslookup -q=AAAA www.ubuntu.com
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
5

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.

Community
  • 1
  • 1
iabdalkader
  • 17,009
  • 4
  • 47
  • 74
  • Hm I have this code in school project and it could be negative evaluated. Because the numbers of allocs and frees are different. ==20489== HEAP SUMMARY: ==20489== in use at exit: 168 bytes in 1 blocks ==20489== total heap usage: 170 allocs, 169 frees, 38,042 bytes allocated – Petr Přikryl Nov 05 '12 at 10:36
  • @PetrPřikryl I don't see any problem in the way you call getaddrinfo, so it's probably local memory allocated for getaddrinfo. it's really common with libraries to do that – iabdalkader Nov 05 '12 at 10:39
  • @PetrPřikryl never mind, I thought you didn't free the memory returned by getaddrinfo, just realized you do. – iabdalkader Nov 05 '12 at 10:47
2

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.

Art
  • 19,807
  • 1
  • 34
  • 60