- 论坛徽章:
- 14
|
VC自带代码级的内存检测
若是MFC工程,那它在生成的代码中已经加了
若是不带MFC的工程,如下- #ifdef _DEBUG
- #define _CRTDBG_MAP_ALLOC // 不加的话,就不知道是哪个文件哪行泄漏。但此中没有new,所以new还需要自己手工加
- #include <crtdbg.h>
- #ifdef new
- #undef new
- #endif
- #define new new(_NORMAL_BLOCK,__FILE__,__LINE__)
- #endif
- #include <cstdlib> // for malloc
- int main()
- {
- malloc( 4 );
- new int(0);
- _CrtDumpMemoryLeaks();
- return 0;
- }
复制代码 按F5调试运行,结果后,在Output窗口显示- Detected memory leaks!
- Dumping objects ->
- d:\sources\cpp001\f.cpp(16) : {119} normal block at 0x003962B8, 4 bytes long.
- Data: < > 00 00 00 00
- d:\sources\cpp001\f.cpp(15) : {118} normal block at 0x00396278, 4 bytes long.
- Data: < > CD CD CD CD
- Object dump complete.
复制代码 |
|