按照每個問題,我遇到了一些內存泄漏getnameinfo。我正在使用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)和gcc 版本4.6.3。
我正在鏈接我的服務器可執行文件g ++到目前爲止valgrind還沒有報告過問題。然後,我添加了一個簡單的電話getnameinfo打印出什麼是連接客戶端的網絡名稱和端口。
我得到如下結果: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: [email protected]@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);
}
有你有泄漏...
我可以證實,該泄漏發生:6個連客戶端的valgrind發現泄漏的60個字節(我猜客戶是從同一個主機連接的,所以如果與主機名相關的話,增長率與預期一致)。
有什麼想法?
乾杯
可以隨時下載源代碼並查看它在做什麼。 –
而且由於它是開源的,你可以自己修復這個問題! –
可能必須這樣做:-) – Emanuele