2009-09-18 43 views
4

我使用getpwnam_r來處理我的程序中的客戶端連接。可悲的是,它似乎分配了一個永遠不會釋放的緩衝區。 有關Valgrind的輸出:getpwnam_r內存泄漏


==15774== 536 (104 direct, 432 indirect) bytes in 2 blocks are definitely lost in loss record 1 of 3 
==15774== at 0x4C24CFE: malloc (in /usr/lib64/valgrind/amd64-linux/vgpreload_memcheck.so) 
==15774== by 0x5143B5A: nss_parse_service_list (in /lib64/libc-2.10.1.so) 
==15774== by 0x51442E6: __nss_database_lookup (in /lib64/libc-2.10.1.so) 
==15774== by 0x57BE35F: ??? 
==15774== by 0x57BF3F6: ??? 
==15774== by 0x51014D2: [email protected]@GLIBC_2.2.5 (in /lib64/libc-2.10.1.so) 
==15774== by 0x407906: dopass (auth.c:71) 
==15774== by 0x40393E: do_cmd (command.c:127) 
==15774== by 0x402496: ftp_main (server.c:58) 
==15774== by 0x40224C: handle_client (daemon.c:211) 
==15774== by 0x402073: daemon_main (daemon.c:123) 
==15774== by 0x4043CC: main (main.c:48) 
==15774== 
==15774== LEAK SUMMARY: 
==15774== definitely lost: 104 bytes in 2 blocks. 
==15774== indirectly lost: 432 bytes in 18 blocks. 
==15774==  possibly lost: 0 bytes in 0 blocks. 
==15774== still reachable: 0 bytes in 0 blocks. 
==15774==   suppressed: 0 bytes in 0 blocks. 

伊勢有沒有辦法告訴getpwnam_r釋放它的緩衝區?或者我將不得不壓制這些Valgrind錯誤?

謝謝,卡斯帕

+0

我認爲這是重複的,請看這裏:http://stackoverflow.com/questions/188591/is-there-a-fix-or-a-workaround-for-the-memory-leak-in-getpwnam/266785 – schnaader 2009-09-18 22:20:01

+0

@schnaader:更好地看待問題。這不是重複的,因爲getpwnam遭受與此函數完全相同的內存泄漏。剛纔提到的getpwnam_r靜態數據getpwnam分配的問題沒有。 – kmm 2009-09-18 22:28:05

回答

7

內存並沒有專門爲getpwnam分配。相反,它代表​​的配置。 AFAICT,只有這樣纔能有通過調用__libc_freeres的libc發佈這款內存:

extern void __libc_freeres (void); 

這應該釋放所有內存C庫保存到(NSS持有到不只是內存)。