Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getaddrinfo memory leak

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?

like image 313
Petr Přikryl Avatar asked Nov 05 '12 10:11

Petr Přikryl


People also ask

Is memory leak serious?

Very dangerous. Memory leaks in the kernel level lead to serious system stability issues. Kernel memory is very limited compared to user land memory and should be handled cautiously. Memory is allocated but never freed.

Do memory leaks cause permanent damage?

Physical or permanent damage does not happen from memory leaks. Memory leaks are strictly a software issue, causing performance to slow down among applications within a given system. It should be noted a program taking up a lot of RAM space is not an indicator that memory is leaking.

What causes a memory leak?

DEFINITION A memory leak is the gradual deterioration of system performance that occurs over time as the result of the fragmentation of a computer's RAM due to poorly designed or programmed applications that fail to free up memory segments when they are no longer needed.

What happens when memory is leaked?

A memory leak reduces the performance of the computer by reducing the amount of available memory. Eventually, in the worst case, too much of the available memory may become allocated and all or part of the system or device stops working correctly, the application fails, or the system slows down vastly due to thrashing.


2 Answers

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
like image 194
Jon Ander Ortiz Durántez Avatar answered Oct 01 '22 01:10

Jon Ander Ortiz Durántez


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.

like image 44
iabdalkader Avatar answered Oct 01 '22 02:10

iabdalkader