記憶體泄露偵查工具LeakDiag的使用 .

來源:互聯網
上載者:User

LeakDiag是微軟一款檢測memory leak的工具,使用比較簡單

首先去下載一個ftp://ftp.microsoft.com/PSS/Tools/Developer%20Support%20Tools/LeakDiag/

安裝好,預設是在c:/leakdiag



然後寫一個測試程式來測試
#include < stdio.h >
#include < windows.h >

int main()
{
     while ( 1 )
     {
         char * c = new char [ 10 ];
         printf( " %s " ,c);
         Sleep( 1000 );
     }
     return 0 ;
}


在LeakDiag中選擇aaa.exe
然後選擇Windows Heap Allocator來跟蹤heap的使用,按start開始,等一會按log,然後再stop
會在c:/leakdiag/Logs下面產生一個log

關鍵的部分是 < LEAKS ver ="1.25.28.2201" >
< STACK numallocs ="05" size ="048" totalsize ="0240" >
< STACKSTATS >
< SIZESTAT size ="048" numallocs ="05" />
< HEAPSTAT handle ="340000" numallocs ="05" />
</ STACKSTATS >
< FRAME num ="0" dll ="aaa.exe" function ="" filename ="" line ="" addr ="0x407a72" offset ="0x00007A72" />
< FRAME num ="1" dll ="aaa.exe" function ="" filename ="" line ="" addr ="0x402ff2" offset ="0x00002FF2" />
< FRAME num ="2" dll ="aaa.exe" function ="" filename ="" line ="" addr ="0x402df9" offset ="0x00002DF9" />
< FRAME num ="3" dll ="aaa.exe" function ="" filename ="" line ="" addr ="0x402dd6" offset ="0x00002DD6" />
< FRAME num ="4" dll ="aaa.exe" function ="" filename ="" line ="" addr ="0x40112f" offset ="0x0000112F" />
< FRAME num ="5" dll ="aaa.exe" function ="" filename ="" line ="" addr ="0x401038" offset ="0x00001038" />
< FRAME num ="6" dll ="aaa.exe" function ="" filename ="" line ="" addr ="0x401269" offset ="0x00001269" />
< FRAME num ="7" dll ="KERNEL32.dll" function ="GetSortTablesFileInfo" offset ="0xC" filename ="" line ="" addr ="0x7C5989D5" />
< STACKID > 00524008 </ STACKID >
</ STACK >
</ LEAKS >


其中numallocs是分配的次數,size是每次分配的大小,frame則是leak的時候的call stack
這裡的stack只顯示了binary,是因為symbol沒有配好

把對應的pdb檔案路徑加到[Tools]-[Option]-Symbol search path,就能得到 < LEAKS ver ="1.25.28.2201" >
< STACK numallocs ="06" size ="048" totalsize ="0288" >
< STACKSTATS >
< SIZESTAT size ="048" numallocs ="06" />
< HEAPSTAT handle ="340000" numallocs ="06" />
</ STACKSTATS >
< FRAME num ="0" dll ="aaa.exe" function ="_heap_alloc_base" offset ="0xC2" filename ="malloc.c" line ="200" addr ="0x407A72" />
< FRAME num ="1" dll ="aaa.exe" function ="_heap_alloc_dbg" offset ="0x1A2" filename ="dbgheap.c" line ="378" addr ="0x402FF2" />
< FRAME num ="2" dll ="aaa.exe" function ="_nh_malloc_dbg" offset ="0x19" filename ="dbgheap.c" line ="248" addr ="0x402DF9" />
< FRAME num ="3" dll ="aaa.exe" function ="_nh_malloc" offset ="0x16" filename ="dbgheap.c" line ="197" addr ="0x402DD6" />
< FRAME num ="4" dll ="aaa.exe" function ="operator new" offset ="0xF" filename ="new.cpp" line ="24" addr ="0x40112F" />
< FRAME num ="5" dll ="aaa.exe" function ="main" offset ="0x28" filename ="D:/Test/aaa/test.cpp" line ="8" addr ="0x401038" />
< FRAME num ="6" dll ="aaa.exe" function ="mainCRTStartup" offset ="0xE9" filename ="crt0.c" line ="206" addr ="0x401269" />
< STACKID > 00524008 </ STACKID >
</ STACK >
</ LEAKS >


由test.cpp line 8引起的memory leak.
另外,以上leakdiag產生的log資訊可以用LDParser來分析。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.