#include <stdio.h>
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef __cplusplus
#ifdef DEBUG_NEW
#undef DEBUG_NEW
#endif
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
int main(int argc, char* argv[])
{
int *a = (int*)malloc(90);
int *b = new int[10];
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
//_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
//_CrtDumpMemoryLeaks();
return 0;
}
像这个程序一样,在文件前面加这样的代码就可以了
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef __cplusplus
#ifdef DEBUG_NEW
#undef DEBUG_NEW
#endif
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif
本文介绍了一种在C++中使用Microsoft的Crtdbg库进行内存泄漏检查的方法。通过定义宏和条件编译,可以在调试模式下启用内存分配跟踪,帮助开发者定位和解决内存泄漏问题。
1673

被折叠的 条评论
为什么被折叠?



