用c++写程序久了,使用malloc经常忘记了使用free来释放内存。
root@dubuntu:/home/wangyao/Desktop/v2# valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./webspy NBA SINA QQ Sports -nocase
来查看到底是那个函数中malloc了之后,没有进行释放内存。valgrind能够指出相应的行号。
==12766==
==12766== ERROR SUMMARY: 19 errors from 14 contexts (suppressed: 15 from 1)
==12766== malloc/free: in use at exit: 634 bytes in 31 blocks.
==12766== malloc/free: 12,529 allocs, 12,498 frees, 229,053,653 bytes allocated.
==12766== For counts of detected errors, rerun with: -v
==12766== searching for pointers to 31 not-freed blocks.
==12766== checked 141,764 bytes.
==12766==
==12766==
==12766== 4 bytes in 1 blocks are still reachable in loss record 1 of 5
==12766== at 0x401C38B: malloc (vg_replace_malloc.c:149)
==12766== by 0x403B6AC: pcap_alloc (in /usr/lib/libpcap.so.0.9.5)
==12766== by 0x403B789: (within /usr/lib/libpcap.so.0.9.5)
==12766== by 0x403B861: pcap__switch_to_buffer (in /usr/lib/libpcap.so.0.9.5)
==12766== by 0x403B979: pcap__scan_buffer (in /usr/lib/libpcap.so.0.9.5)
==12766== by 0x403B9F6: pcap__scan_bytes (in /usr/lib/libpcap.so.0.9.5)
==12766== by 0x403BA5D: pcap__scan_string (in /usr/lib/libpcap.so.0.9.5)
==12766== by 0x403BA8C: lex_init (in /usr/lib/libpcap.so.0.9.5)
==12766== by 0x402E758: pcap_compile (in /usr/lib/libpcap.so.0.9.5)
==12766== by 0x80490D0: main (entry.c:81)
==12766==
==12766==
==12766== 128 (8 direct, 120 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 5
==12766== at 0x401C38B: malloc (vg_replace_malloc.c:149)
==12766== by 0x804A87D: denylist_add (webspy.c:547)
==12766== by 0x804A80C: denylist_init (webspy.c:526)
==12766== by 0x8048FDD: main (entry.c:66)
==12766==
==12766==
==12766== 120 bytes in 15 blocks are indirectly lost in loss record 3 of 5
==12766== at 0x401C38B: malloc (vg_replace_malloc.c:149)
==12766== by 0x804A8FC: denylist_add (webspy.c:556)
==12766== by 0x804A80C: denylist_init (webspy.c:526)
==12766== by 0x8048FDD: main (entry.c:66)
==12766==
==12766==
==12766== 166 bytes in 13 blocks are still reachable in loss record 4 of 5
==12766== at 0x401C38B: malloc (vg_replace_malloc.c:149)
==12766== by 0x804AA3C: AC_MALLOC (acsmx.c:73)
==12766== by 0x804B019: acsmNew (acsmx.c:353)
==12766== by 0x8048DF0: main (entry.c:47)
==12766==
==12766==
==12766== 336 bytes in 1 blocks are still reachable in loss record 5 of 5
==12766== at 0x401C38B: malloc (vg_replace_malloc.c:149)
==12766== by 0x4036DAE: icode_to_fcode (in /usr/lib/libpcap.so.0.9.5)
==12766== by 0x402E8DE: pcap_compile (in /usr/lib/libpcap.so.0.9.5)
==12766== by 0x80490D0: main (entry.c:81)
==12766==
==12766== LEAK SUMMARY:
==12766== definitely lost: 8 bytes in 1 blocks.
==12766== indirectly lost: 120 bytes in 15 blocks.
==12766== possibly lost: 0 bytes in 0 blocks.
==12766== still reachable: 506 bytes in 15 blocks.
==12766== suppressed: 0 bytes in 0 blocks.
最后,解决了内存泄露的问题,但是不能够单靠valgrind来解决内存泄露问题哦,还是靠程序员自己来解决。
malloc之后一定要free.
阅读(5195) | 评论(0) | 转发(0) |