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...