getnameinfo内存泄漏是否确认?
按照每个问题,我遇到了一些由getnameinfo泄漏的内存。 我正在使用gcc 4.6.3版本的Ubuntu 12.04(Linux scv 3.2.0-35-generic#55 -Ubuntu SMP Wed Dec 5 17:42:16 UTC 2012 x86_64 x86_64 x86_64 GNU / Linux)。
我使用g ++将我的服务器可执行文件链接到目前为止,valgrind尚未报告问题。 然后,我添加了一个简单的调用getnameinfo来打印出连接客户端的网络名称和端口是什么。
我得到以下内容:
==4425== ==4425== HEAP SUMMARY: ==4425== in use at exit: 10 bytes in 1 blocks ==4425== total heap usage: 4,508 allocs, 4,507 frees, 134,939,153 bytes allocated ==4425== ==4425== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==4425== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==4425== by 0x50D7D71: strdup (strdup.c:43) ==4425== by 0x1484B861: ??? ==4425== by 0x515B871: gethostbyaddr_r@@GLIBC_2.2.5 (getXXbyYY_r.c:256) ==4425== by 0x5161D06: getnameinfo (getnameinfo.c:223) ==4425== by 0x404175: solsrv_run (solsrv.c:381) ==4425== by 0x404DAC: main (main.c:167) ==4425== ==4425== LEAK SUMMARY: ==4425== definitely lost: 10 bytes in 1 blocks ==4425== indirectly lost: 0 bytes in 0 blocks ==4425== possibly lost: 0 bytes in 0 blocks ==4425== still reachable: 0 bytes in 0 blocks ==4425== suppressed: 0 bytes in 0 blocks ==4425== ==4425== For counts of detected and suppressed errors, rerun with: -v ==4425== ERROR SUMMARY: 12 errors from 11 contexts (suppressed: 2 from 2)
我究竟做错了什么?
代码简单如下:
struct sockaddr addr;
socklen_t addr_sz = sizeof(addr);
char host[NI_MAXHOST],
serv[NI_MAXSERV];
int infd = accept(srv_fd, (struct sockaddr*)&addr, &addr_sz);
if (infd == -1) {
... manage error on accept ...
}
if(getnameinfo((struct sockaddr *)&addr, addr_sz, host, NI_MAXHOST, serv, NI_MAXSERV, NI_NUMERICSERV)) {
strncpy(host, "<unknown host>", NI_MAXHOST-1);
strncpy(serv, "<unknown port>", NI_MAXSERV-1);
}
在那里你有泄漏...
我可以证实,泄漏发生了 :对于连接valgrind的6个客户端发现了60个字节泄漏(我猜客户端连接的是同一个主机,所以如果它与主机名相关,增长率与预期相符)。
任何想法?
干杯
最终发现真正的泄漏。
连接到服务器套接字时,请使用name.local
而不是localhost
和/或完全限定名称。
getnameinfo()
将会泄漏。
我可以重现的错误12.04
, 12.10
既x64和x86。
如果我连接指定.local
名称它泄漏。
干杯
链接地址: http://www.djcxy.com/p/64943.html上一篇: Is getnameinfo memory leaking confirmed?
下一篇: MPI memory leak