VS2005内存泄漏检测方法
- 格式:doc
- 大小:76.50 KB
- 文档页数:5
VS2005内存泄漏检测方法
2010-03-09 09:13 247人阅读评论(0) 收藏举报VS2005内存泄漏检测方法
非MFC程序可以用以下方法检测内存泄露:
1.程序开始包含如下定义:
view plaincopy to clipboardprint?
1. #ifdef _DEBUG
2. #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
3. #else
4. #define DEBUG_CLIENTBLOCK
5. #endif // _DEBUG
6. #define _CRTDBG_MAP_ALLOC
7. #include
8. #include
9. #ifdef _DEBUG
10. #define new DEBUG_CLIENTBLOCK
11. #endif // _DEBUG
2.程序中添加下面的函数:
view plaincopy to clipboardprint?
1. _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
1. #ifdef _DEBUG
2. protected:
3. CMemoryState m_msOld, m_msNew, m_msDiff;
4. #endif // _DEBUG
1. #ifdef _DEBUG
2. m_msOld.Checkpoint();
3. #endif // _DEBUG
4.
5.
3.在 CMyApp::ExitInstance() 中添加如下代码:
view plaincopy to clipboardprint?
1. #ifdef _DEBUG
2. m_msNew.Checkpoint();
3. if (m_msDiff.Difference(m_msOld, m_msNew))
4. {
5. afxDump<<"/nMemory Leaked :/n";
6. m_msDiff.DumpStatistics();
7. afxDump<<"Dump Complete !/n/n";
8. }
9. #endif // _DEBUG
0 bytes in 0 Client Blocks.
Largest number used: 8825 bytes.
Total allocations: 47506 bytes.
Dump Complete !
Detected memory leaks!
Dumping objects ->
g:/programs/chat/chatdlg.cpp(120) : {118} normal block at 0x00D98150, 8 bytes long. Data: < > A8 7F D9 00 01 00 00 00
Object dump complete.