在调试MFC程序时,程序退出时,如果有内存泄漏,Debug窗口会输出类似下面的信息
Detected memory leaks!
Dumping objects ->
E:\\StaticLib\\MFCTest\\MFCTest.cpp(255) : {63} normal block at 0x00372EA0, 10 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD
Object dump complete.
非MFC程序可以用_CrtDumpMemoryLeaks()函数检测内存泄漏,但无法输出代码行信息:
Detected memory leaks!
Dumping objects ->
{55} normal block at 0x00371000, 1 bytes long.
Data: <a> 61
如何能够显示内存泄漏的代码行呢?方法是重新定义new如下,以便告诉compiler使用debug版本的operator new()方法:
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
Detected memory leaks!
Dumping objects ->
E:\\StaticLib\\altSec\\altSec.cpp(51) : {55} normal block at 0x00371000, 1 bytes long.
Data: <a> 61
相关VC源代码位置:
CRT\\SRC\\dbgnew.cpp
CRT\\SRC\\new.cpp