2

I've stripped down my code to the bare minimum and, despite setting servinfo to NULL and excessively calling freeaddrinfo, I'm still seeing a memory leak with valgrind --leak-check=yes. I found this thread but I wasn't able to resolve the issue.

Here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netdb.h>

int main() {
  /* Declare connection variables */
  struct addrinfo hints, *servinfo;
  char port[6];
  servinfo = NULL;

  /* Set address attributes */
  memset(&hints, 0, sizeof hints);
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  sprintf(port, "%d", 8888);
  if (getaddrinfo(NULL, port, &hints, &servinfo) != 0) {
    freeaddrinfo(servinfo);
    fprintf(stderr, "> Error: getaddrinfo() failed.\n");
    exit(EXIT_FAILURE);
  }
  freeaddrinfo(servinfo);

  return 0;
}

And here's the valgrind output:

==5199== 640 (320 direct, 320 indirect) bytes in 1 blocks are definitely lost in loss record 104 of 122
==5199==    at 0x100007351: malloc (vg_replace_malloc.c:303)
==5199==    by 0x10038EA3D: sa_dst_lookup (in /usr/lib/system/libsystem_network.dylib)
==5199==    by 0x10038E1F6: sa_dst_compare_internal (in /usr/lib/system/libsystem_network.dylib)
==5199==    by 0x1002144DE: _qsort (in /usr/lib/system/libsystem_c.dylib)
==5199==    by 0x1002B5F68: _gai_sort_list (in /usr/lib/system/libsystem_info.dylib)
==5199==    by 0x1002ADD30: si_addrinfo (in /usr/lib/system/libsystem_info.dylib)
==5199==    by 0x1002AD966: getaddrinfo (in /usr/lib/system/libsystem_info.dylib)
==5199==    by 0x100000EBC: main (test.c:19)

Any ideas? Could it be an OS X Yosemite issue? Thanks for your help...

Community
  • 1
  • 1
  • 1
    I can't reproduce the issue on my computer (linux 4.1 with gcc 5.2.0). Valgrind reports no leaks at all with the code sample you provided (`All heap blocks were freed -- no leaks are possible`). So I suggest there might be a leak in the implementation of `getaddrinfo` you are using. – or1426 Sep 02 '15 at 22:21
  • Yup, looks like an error in the implementation. Switched over to an Ubuntu VM and the leaks are gone. That's a good hour wasted... but I won't be making that mistake again! :) –  Sep 02 '15 at 22:37
  • Usually these are known issues, e.g a free of some memory is done after `valgrind` can detect it. The difference that you observe between different systems is in fact an exception list for `valgrind` of errors that it should suppress. So not the reality changes between the two, just your knowledge about it. – Jens Gustedt Sep 02 '15 at 23:11
  • 1
    Valgrind has preliminary support only for modern OS X. This situation will be significantly improved in the next Valgrind release, but for now there are a number of known leaks reported in system libraries (i.e. outside your control). – e76d587d9 Sep 03 '15 at 05:51

0 Answers0