我已经将我的代码简化为最低限度,尽管将servinfo设置为NULL并且过度调用freeaddrinfo,但我仍然看到valgrind的内存泄漏--leak-check = yes。我找到了this thread,但我无法解决问题。
以下是代码:
#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;
}
这是valgrind输出:
==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)
有什么想法吗?它可能是OS X Yosemite问题吗?谢谢你的帮助...