用UMDH检查C++程序内存泄露

编写一个MFC测试程序CheckMemoryLeak来做实验,在该测试程序中分别加入两个按钮,用于申请内存,但不释放,为了在没有VS环境下检查内存泄露,可以使用UMDH工具,该工具在Windbg同一个目录下。

void CCheckMemoryLeakDlg::OnBnClickedButton1()
{
 char* strBuffer = new char[100];
}

void CCheckMemoryLeakDlg::OnBnClickedButton2()
{
 char* strBuffer = (char*)malloc(100);

}

 

启动命令输入控制台:

//1:

D:/Program Files/Microsoft SDKs/Windows/v6.0>cd /d D:/Program Files/Debugging Tools for Windows (x86) 

//2:

D:/Program Files/Debugging Tools for Windows (x86)>set _NT_SYMBOL_PATH=F:/MicrosoftSymbols 

 //3:

D:/Program Files/Debugging Tools for Windows (x86)>gflags /i checkmemoryleak.exe +ust
Current Registry Settings for checkmemoryleak.exe executable are: 00001000
    ust - Create user mode stack trace database
 //4:

运行CheckMemoryLeak.exe

//5:
D:/Program Files/Debugging Tools for Windows (x86)>umdh.exe -pn:CheckMemoryLeak.exe -f:Snap1.log

//6:

点击Button1和Button2申请内存

 //7:

D:/Program Files/Debugging Tools for Windows (x86)>umdh.exe -pn:CheckMemoryLeak.exe -f:Snap2.log

 //8:

比较snap1和snap2,并把比较结果存入result.txt 

D:/Program Files/Debugging Tools for Windows (x86)>umdh -d snap1.log snap2.log -f:result.txt


打开result.txt,可以看到这两个函数存在内存泄露

 

// Debug library initialized ...
DBGHELP: CheckMemoryLeak - private symbols & lines 
         E:/TestProj/CheckMemoryLeak/Release/CheckMemoryLeak.pdb
DBGHELP: ntdll - public symbols  
         F:/MicrosoftSymbols/ntdll.pdb/C0A498F0036E4D4FB5CBF69005B0F9242/ntdll.pdb
DBGHELP: kernel32 - public symbols  
         F:/MicrosoftSymbols/kernel32.pdb/F8A69C9369C04BF1835583337A60C1132/kernel32.pdb
DBGHELP: mfc90u - private symbols & lines 
         F:/MicrosoftSymbols/mfc90u.i386.pdb/C75B10471EEA4AC1B7C7F62A3088570A16/mfc90u.i386.pdb
DBGHELP: MSVCR90 - private symbols & lines 
         F:/MicrosoftSymbols/msvcr90.i386.pdb/1AB1131312CE4769A8D4E2CC55A20B5D1/msvcr90.i386.pdb
DBGHELP: USER32 - public symbols  
         F:/MicrosoftSymbols/user32.pdb/25BD0FF156674BA4B387F2F92E0BDF862/user32.pdb
DBGHELP: GDI32 - public symbols  
         F:/MicrosoftSymbols/gdi32.pdb/6DFA21C671C94F7B8C895656B3B3F3062/gdi32.pdb
DBGHELP: ADVAPI32 - public symbols  
         F:/MicrosoftSymbols/advapi32.pdb/19D721512AA34917998058CB02F5E5F92/advapi32.pdb
DBGHELP: RPCRT4 - public symbols  
         F:/MicrosoftSymbols/rpcrt4.pdb/000F0A9AAEF746EEB98F5A50153E07EA2/rpcrt4.pdb
DBGHELP: SHLWAPI - public symbols  
         F:/MicrosoftSymbols/shlwapi.pdb/7D3C64434A3248EA958A1352DAE70CC52/shlwapi.pdb
DBGHELP: msvcrt - public symbols  
         F:/MicrosoftSymbols/msvcrt.pdb/8A24BF4B1A05412FB0312AD4CB7867042/msvcrt.pdb
DBGHELP: COMCTL32 - public symbols  
         F:/MicrosoftSymbols/comctl32.pdb/21E9875AABA84F2B8684918034532B032/comctl32.pdb
DBGHELP: MSIMG32 - public symbols  
         F:/MicrosoftSymbols/msimg32.pdb/F3CCF1CDEF724758A292CC67212D6C0C2/msimg32.pdb
DBGHELP: IMM32 - public symbols  
         F:/MicrosoftSymbols/imm32.pdb/F08E00C515AF488C9D89C9EFFED71E292/imm32.pdb
DBGHELP: MSCTF - public symbols  
         F:/MicrosoftSymbols/msctf.pdb/1377BE94C653479BA027ADFEF2014B032/msctf.pdb
DBGHELP: LPK - public symbols  
         F:/MicrosoftSymbols/lpk.pdb/93FD32A77B76490BA5D331F998C5751F2/lpk.pdb
DBGHELP: USP10 - public symbols  
         F:/MicrosoftSymbols/usp10.pdb/676B6AF372034FB1A49400E8A5BAA1522/usp10.pdb
DBGHELP: SOPHOS~1 - export symbols
DBGHELP: PSAPI - public symbols  
         F:/MicrosoftSymbols/psapi.pdb/FA398191D257469CBA795A86B704D9B92/psapi.pdb
DBGHELP: UxTheme - public symbols  
         F:/MicrosoftSymbols/UxTheme.pdb/7785CC6B21A548F9813607AF098FDC802/UxTheme.pdb
DBGHELP: dwmapi - public symbols  
         F:/MicrosoftSymbols/dwmapi.pdb/36E9264C14DE4414A7711245A90F86B62/dwmapi.pdb
DBGHELP: MFC90ENU - no symbols loaded
DBGHELP: shell32 - public symbols  
         F:/MicrosoftSymbols/shell32.pdb/22FD88038D7D42E59A29B4E7802A5FC02/shell32.pdb
DBGHELP: ole32 - public symbols  
         F:/MicrosoftSymbols/ole32.pdb/6C8339F7D13A438490BCD30C382F51202/ole32.pdb
DBGHELP: OLEAUT32 - public symbols  
         F:/MicrosoftSymbols/oleaut32.pdb/EC770B82C6444AADB5B8C3BDB29D6CF92/oleaut32.pdb
//                                                                          
// Each log entry has the following syntax:                                 
//                                                                          
// + BYTES_DELTA (NEW_BYTES - OLD_BYTES) NEW_COUNT allocs BackTrace TRACEID 
// + COUNT_DELTA (NEW_COUNT - OLD_COUNT) BackTrace TRACEID allocations      
//     ... stack trace ...                                                  
//                                                                          
// where:                                                                   
//                                                                          
//     BYTES_DELTA - increase in bytes between before and after log         
//     NEW_BYTES - bytes in after log                                       
//     OLD_BYTES - bytes in before log                                      
//     COUNT_DELTA - increase in allocations between before and after log   
//     NEW_COUNT - number of allocations in after log                       
//     OLD_COUNT - number of allocations in before log                      
//     TRACEID - decimal index of the stack trace in the trace database     
//         (can be used to search for allocation instances in the original  
//         UMDH logs).                                                      
//                                                                         


+     200 (    200 -      0)      2 allocs BackTrace8CD380
+       2 (      2 -      0) BackTrace8CD380 allocations

 ntdll!RtlAllocateHeap+0000021D
 MSVCR90!malloc+00000079 (f:/dd/vctools/crt_bld/self_x86/crt/src/malloc.c, 163)
 mfc90u!operator new+00000033 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/afxmem.cpp, 349)
 CheckMemoryLeak!CCheckMemoryLeakDlg::OnBnClickedButton1+00000007 (e:/testproj/checkmemoryleak/checkmemoryleak/checkmemoryleakdlg.cpp, 160)
 mfc90u!CCmdTarget::OnCmdMsg+00000124 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/cmdtarg.cpp, 381)
 mfc90u!CPropertySheet::OnCmdMsg+0000001D (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/dlgprop.cpp, 814)
 mfc90u!CWnd::OnCommand+00000092 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 2363)
 mfc90u!CWnd::OnWndMsg+00000066 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 1769)
 mfc90u!CWnd::WindowProc+00000024 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 1755)
 mfc90u!AfxCallWndProc+000000A3 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 240)
 mfc90u!AfxWndProc+00000037 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 402)
 mfc90u!AfxWndProcBase+00000056 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/afxstate.cpp, 441)
 USER32!InternalCallWinProc+00000023
 USER32!UserCallWinProcCheckWow+0000014B
 USER32!SendMessageWorker+000004B7
 USER32!SendMessageW+0000007C
 COMCTL32!Button_NotifyParent+0000003D
 COMCTL32!Button_ReleaseCapture+00000112
 COMCTL32!Button_WndProc+00000A4B
 USER32!InternalCallWinProc+00000023
 USER32!UserCallWinProcCheckWow+0000014B
 USER32!DispatchMessageWorker+00000322
 USER32!DispatchMessageW+0000000F
 USER32!IsDialogMessageW+00000586
 mfc90u!CWnd::IsDialogMessageW+00000032 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/winocc.cpp, 197)
 mfc90u!CWnd::PreTranslateInput+0000002D (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 4331)
 mfc90u!CDialog::PreTranslateMessage+00000095 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/dlgcore.cpp, 79)
 mfc90u!CWnd::WalkPreTranslateTree+00000023 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 2946)
 mfc90u!AfxInternalPreTranslateMessage+00000041 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/thrdcore.cpp, 233)
 mfc90u!CWinThread::PreTranslateMessage+0000000D (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/thrdcore.cpp, 777)
 mfc90u!AfxPreTranslateMessage+00000019 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/thrdcore.cpp, 255)
 mfc90u!AfxInternalPumpMessage+0000002D (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/thrdcore.cpp, 178)

+     100 (    100 -      0)      1 allocs BackTrace8CD5BC
+       1 (      1 -      0) BackTrace8CD5BC allocations

 ntdll!RtlAllocateHeap+0000021D
 MSVCR90!malloc+00000079 (f:/dd/vctools/crt_bld/self_x86/crt/src/malloc.c, 163)
 CheckMemoryLeak!CCheckMemoryLeakDlg::OnBnClickedButton2+00000008 (e:/testproj/checkmemoryleak/checkmemoryleak/checkmemoryleakdlg.cpp, 166)
 mfc90u!CCmdTarget::OnCmdMsg+00000124 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/cmdtarg.cpp, 381)
 mfc90u!CPropertySheet::OnCmdMsg+0000001D (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/dlgprop.cpp, 814)
 mfc90u!CWnd::OnCommand+00000092 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 2363)
 mfc90u!CWnd::OnWndMsg+00000066 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 1769)
 mfc90u!CWnd::WindowProc+00000024 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 1755)
 mfc90u!AfxCallWndProc+000000A3 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 240)
 mfc90u!AfxWndProc+00000037 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 402)
 mfc90u!AfxWndProcBase+00000056 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/afxstate.cpp, 441)
 USER32!InternalCallWinProc+00000023
 USER32!UserCallWinProcCheckWow+0000014B
 USER32!SendMessageWorker+000004B7
 USER32!SendMessageW+0000007C
 COMCTL32!Button_NotifyParent+0000003D
 COMCTL32!Button_ReleaseCapture+00000112
 COMCTL32!Button_WndProc+00000A4B
 USER32!InternalCallWinProc+00000023
 USER32!UserCallWinProcCheckWow+0000014B
 USER32!DispatchMessageWorker+00000322
 USER32!DispatchMessageW+0000000F
 USER32!IsDialogMessageW+00000586
 mfc90u!CWnd::IsDialogMessageW+00000032 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/winocc.cpp, 197)
 mfc90u!CWnd::PreTranslateInput+0000002D (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 4331)
 mfc90u!CDialog::PreTranslateMessage+00000095 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/dlgcore.cpp, 79)
 mfc90u!CWnd::WalkPreTranslateTree+00000023 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/wincore.cpp, 2946)
 mfc90u!AfxInternalPreTranslateMessage+00000041 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/thrdcore.cpp, 233)
 mfc90u!CWinThread::PreTranslateMessage+0000000D (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/thrdcore.cpp, 777)
 mfc90u!AfxPreTranslateMessage+00000019 (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/thrdcore.cpp, 255)
 mfc90u!AfxInternalPumpMessage+0000002D (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/thrdcore.cpp, 178)
 mfc90u!CDialog::DoModal+0000012A (f:/dd/vctools/vc7libs/ship/atlmfc/src/mfc/dlgcore.cpp, 584)


Total increase ==    300 requested +     92 overhead =    392


from:http://blog.csdn.net/liuyan4794/article/details/5326366

你可能感兴趣的:(用UMDH检查C++程序内存泄露)