首頁: http://valgrind.org/
文檔下載:http://valgrind.org/docs/download_docs.html
簡單使用: http://cs.ecs.baylor.edu/~donahoo/tools/valgrind/
valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./test
Valgrind is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools.
The Valgrind distribution currently includes six production-quality tools: a memory error detector, two thread error detectors, a cache and branch-prediction profiler, a call-graph generating cache and branch-prediction profiler, and a heap profiler. It also includes three experimental tools: a heap/stack/global array overrun detector, a second heap profiler that examines how heap blocks are used, and a SimPoint basic block vector generator. It runs on the following platforms: X86/Linux, AMD64/Linux, ARM/Linux, PPC32/Linux, PPC64/Linux, S390X/Linux, MIPS/Linux, ARM/Android (2.3.x and later), X86/Android (4.0 and later), X86/Darwin and AMD64/Darwin (Mac OS X 10.6 and 10.7, with limited support for 10.8).
當原始碼使用 -g 選項編時,valgrind 可以定位出現記憶體流失的行號, 無-g選項也是可以的。
如: g++ -g HasPtr.cxx
/* * HasPtr.cxx * */#include <iostream>using namespace std;class HasPtr{public://預設建構函式HasPtr(){cout<<"HasPtr預設建構函式被調用"<<hex<<this<<dec<<endl;//pval = new int(0); //#1pval = NULL;val = 0;}//正常建構函式HasPtr(const int * rhs,int ival = 0){cout<<"HasPtr建構函式被調用,this@"<<hex<<this<<dec<<endl;pval = new int(*rhs);val = ival;}//複製建構函式HasPtr(const HasPtr & rhs){cout<<"HasPtr複製建構函式被調用,this@"<<hex<<this<<dec<<endl;pval = new int(*rhs.pval);val = rhs.val;}//賦值操作符HasPtr & operator = (const HasPtr & rhs){cout<<"HasPtr operator = 函數被調用,this@"<<hex<<this<<dec<<endl;//*pval = *rhs.pval;//#1處,在預設構造中也有分配指標所指對象,則直接賦值/***************************************************/delete pval;//刪除NULL指標是安全的,注釋掉這行,可以查看到記憶體流失/***************************************************/pval = new int(*rhs.pval);val = rhs.val;return *this;}//建構函式~HasPtr(){delete pval;cout<<"HasPtr析造函數被調用,this@"<<hex<<this<<dec<<endl;}void to_str(){cout<<"HasPtr.to_str() called:val="<<val<<",*pval="<<*pval<<",pval="<<hex<<pval<<",this@"<<hex<<this<<dec<<endl<<endl;}//private:int val,*pval;};int main(int argc, char **argv){int i =110;HasPtr p1(&i,2);p1.to_str();//HasPtr p2(&i,2);//常規建構函式//HasPtr p2(p1);//複製建構函式//HasPtr p2 = p1;//複製建構函式HasPtr p2;//預設構造p2 = p1;//賦值操作符p2.to_str();p2.val *=2;*p2.pval *=2;cout<<"########## after change ##########"<<endl;p1.to_str();p2.to_str();p1 = p2;cout<<"######## p1 = p2 ############"<<endl;p1.to_str();p2.to_str();return 0;}
檢測結如下:
spark@spark-vbox:~/workspace/Cpp$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes --num-callers=20 --track-fds=yes ./a.out==5837== Memcheck, a memory error detector==5837== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.==5837== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info==5837== Command: ./a.out==5837== HasPtr建構函式被調用,this@0xbee6204cHasPtr.to_str() called:val=2,*pval=110,pval=0x4323028,this@0xbee6204cHasPtr預設建構函式被調用0xbee62054HasPtr operator = 函數被調用,this@0xbee62054HasPtr.to_str() called:val=2,*pval=110,pval=0x4323060,this@0xbee62054########## after change ##########HasPtr.to_str() called:val=2,*pval=110,pval=0x4323028,this@0xbee6204cHasPtr.to_str() called:val=4,*pval=220,pval=0x4323060,this@0xbee62054HasPtr operator = 函數被調用,this@0xbee6204c######## p1 = p2 ############HasPtr.to_str() called:val=4,*pval=220,pval=0x4323098,this@0xbee6204cHasPtr.to_str() called:val=4,*pval=220,pval=0x4323060,this@0xbee62054HasPtr析造函數被調用,this@0xbee62054HasPtr析造函數被調用,this@0xbee6204c==5837== ==5837== FILE DESCRIPTORS: 3 open at exit.==5837== Open file descriptor 2: /dev/pts/2==5837== <inherited from parent>==5837== ==5837== Open file descriptor 1: /dev/pts/2==5837== <inherited from parent>==5837== ==5837== Open file descriptor 0: /dev/pts/2==5837== <inherited from parent>==5837== ==5837== ==5837== HEAP SUMMARY:==5837== in use at exit: 4 bytes in 1 blocks==5837== total heap usage: 3 allocs, 2 frees, 12 bytes allocated==5837== ==5837== 4 bytes in 1 blocks are definitely lost in loss record 1 of 1==5837== at 0x402B9B4: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)==5837== by 0x8048B86: HasPtr::HasPtr(int const*, int) (HasPtr.cxx:20)==5837== by 0x8048821: main (HasPtr.cxx:58)==5837== ==5837== LEAK SUMMARY:==5837== definitely lost: 4 bytes in 1 blocks==5837== indirectly lost: 0 bytes in 0 blocks==5837== possibly lost: 0 bytes in 0 blocks==5837== still reachable: 0 bytes in 0 blocks==5837== suppressed: 0 bytes in 0 blocks==5837== ==5837== For counts of detected and suppressed errors, rerun with: -v==5837== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
它可以檢測出在哪一行分配的記憶體沒有被釋放,不能給出合適的應該釋放的位置,這確實難辦到,分析時要注意。