总结:valgrind 感觉比memwatch好用
====================================================================================
valgrind的使用
下载地址 http://valgrind.org/downloads/current.html#current
下载 Valgrind 3.8.1 解压缩后
$cd Valgrind 3.8.1
$.configure
$make
$make check
#sudo make install
main.cpp
#include<stdio.h> #include<stdlib.h> #include "test.h" int main() { test(); return 0; }
#include<stdio.h> #include<stdlib.h> #include "/tmp/memwatch/memwatch.h" #include "test.h" void test() { char *p1; char *p2; p1 = (char *) malloc(512); p2 = (char *) malloc(512); p1 = p2; free(p1); free(p2); return; }
#ifndef __TEST_H__ #define __TEST_H__ void test(); #endif
Makefile:
final: test.h test.cpp main.cpp /tmp/memwatch/memwatch.c g++ -g test.h test.cpp /tmp/memwatch/memwatch.c main.cpp -DMW_STDIO -o final clean: rm -rf final
执行该文件:
valgrind --tool=memcheck --leak-check=yes ./final 2> lost.log
产生日志如下:
lost.log
==17619== Memcheck, a memory error detector ==17619== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al. ==17619== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info ==17619== Command: ./final ==17619== ==17619== Invalid free() / delete / delete[] / realloc() ==17619== at 0x4025FE9: free (vg_replace_malloc.c:446) ==17619== by 0x8048C33: test() (test.cpp:17) ==17619== by 0x804D9BA: main (main.cpp:6) ==17619== Address 0x42d5258 is 0 bytes inside a block of size 512 free'd ==17619== at 0x4025FE9: free (vg_replace_malloc.c:446) ==17619== by 0x8048C28: test() (test.cpp:16) ==17619== by 0x804D9BA: main (main.cpp:6) ==17619== ==17619== ==17619== HEAP SUMMARY: ==17619== in use at exit: 512 bytes in 1 blocks ==17619== total heap usage: 2 allocs, 2 frees, 1,024 bytes allocated ==17619== ==17619== 512 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==17619== at 0x40265DC: malloc (vg_replace_malloc.c:270) ==17619== by 0x8048C05: test() (test.cpp:11) ==17619== by 0x804D9BA: main (main.cpp:6) ==17619== ==17619== LEAK SUMMARY: ==17619== definitely lost: 512 bytes in 1 blocks ==17619== indirectly lost: 0 bytes in 0 blocks ==17619== possibly lost: 0 bytes in 0 blocks ==17619== still reachable: 0 bytes in 0 blocks ==17619== suppressed: 0 bytes in 0 blocks ==17619== ==17619== For counts of detected and suppressed errors, rerun with: -v ==17619== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 17 from 6)
上面的代码分别给字符指针 p1 和 p2 分配了两个 512 字节的内存块,然后将指向第一个内存块的指针设置为指向第二个内存块。
结果是,第二个内存块的地址丢失了,并导致内存泄漏。
相关参考链接:http://www.phpfans.net/article/htmls/201008/Mjk4OTA5.html
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
MEMWATCH
MEMWATCH 由 Johan Lindh 编写,是一个开放源代码 C 语言内存错误检测工具,您可以自己下载它(请参阅本文后面部分的参考资料)。
只要在代码中添加一个头文件并在 gcc 语句中定义了 MEMWATCH 之后,您就可以跟踪程序中的内存泄漏和错误了。MEMWATCH 支持
ANSI C,它提供结果日志纪录,能检测双重释放(double-free)、错误释放(erroneous free)、没有释放的内存(unfreed memory)、
溢出和下溢等等。
1. 下载Memwatch, http://www.linkdata.se/;
2. 解压后可以看到memwatch.c、memwatch.h、test.c等文件;
2.1 首先运行源代码中的事例程序,检测是否捕捉到内存错误,具体步骤如下:
Linux and other *nixes with gcc:
gcc -o test -DMEMWATCH -DMEMWATCH_STDIO test.c memwatch.c
Windows 95, Windows NT with MS Visual C++:
cl -DMEMWATCH -DMEMWATCH_STDIO test.c memwatch.c
Then simply run the test program.
./test
成功运行后,会看到一个名为"memwatch.log" 的文件,里面记录了内存泄漏的具体情况。
3. 上面事例运行成功后,就可以将测试自己的代码了。
3.1 首先将memwatch.c、memwatch.h复制到被测代码中。
3.2 将源码中每个文件都包含memwatch.h;如果源码中的所有文件都用到了一个文件,也可以将其添加到该文件中。
3.3 重新编译源代码,并在gcc选项中使用 –DMEMWATCH,如果需要对出错时进行控制,可以添加-DMEMWATCH_STDIO 选项,
此时调试出错时可以在标准输出上看到"Abort, Retry,Ignore?”
3.4 编译成功后,运行程序!如果操作无误,可以memwatch.log,里面记录了内存调试的具体情况。
4. 容易出现的问题
4.1 在memwatch.h之后包含string.h时,编译时提示strdup()出错!
解决办法:可以将string.h放置在memwatch.h之前;也可以修改memwatch.h,使其包含
string.h.
4.2 运行程序后,没有出现memwatch.log文件
解决办法:在编译时可能没有定义MEMWATCH;也可能是有些文件没有包含memwatch.h;查看后解决
总结:MEMWATCH 为您显示真正导致问题的行。如果您释放一个已经释放过的指针,它会告诉您。对于没有释放的内存也一样。日
志结尾部分显示统计信息,包括泄漏了多少内存,使用了多少内存,以及总共分配了多少内存。
相关参考链接:http://blog.csdn.net/lengxingfei/archive/2006/08/09/1040800.aspx