Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5771828
  • 博文数量: 675
  • 博客积分: 20301
  • 博客等级: 上将
  • 技术积分: 7671
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-31 16:15
文章分类

全部博文(675)

文章存档

2012年(1)

2011年(20)

2010年(14)

2009年(63)

2008年(118)

2007年(141)

2006年(318)

分类: C/C++

2007-05-20 13:06:37

用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.
阅读(5209) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~