全部在App中完成
1.在 App.h 头文件声明
#ifdef _DEBUG
protected:
CMemoryState m_msOld, m_msNew, m_msDiff;
#endif // _DEBUG
2.在 App::InitInstance() 中添加如下代码:
#ifdef _DEBUG
m_msOld.Checkpoint();
#endif // _DEBUG
3.在 App::ExitInstance() 中添加如下代码:
#ifdef _DEBUG
m_msNew.Checkpoint();
if (m_msDiff.Difference(m_msOld, m_msNew))
{
afxDump<<"\nMemory Leaked :\n";
m_msDiff.DumpStatistics();
afxDump<<"Dump Complete !\n\n";
}
#endif // _DEBUG
Debug模式下运行,然后正常退出exe,
输出:(直接双击红色部分会定位到没有释放内存的地方)
Memory Leaked :
0 bytes in 0 Free Blocks.
-2431 bytes in -80 Normal Blocks.
0 bytes in 0 CRT Blocks.
0 bytes in 0 Ignore Blocks.
-41044 bytes in -4 Client Blocks.
Largest number used: 0 bytes.
Total allocations: 38519 bytes.
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dumpcont.cpp(23) : atlTraceGeneral - Dump Complete !
Detected memory leaks!
Dumping objects ->
e:\2013vs\中文版\csx306a\csx306a\csx306aview.cpp(468) : {1398} client block at 0x003E49D0, subtype c0, 8 bytes long.
f:\dd\vctools\vc7libs\ship\atlmfc\src\mfc\dumpcont.cpp(23) : atlTraceGeneral - a CFont object at $003E49D0, 8 bytes long
Object dump complete.
http://blog.csdn.net/bigtree_mfc/article/details/50323815