转载:http://www.cnblogs.com/zouzf/p/4152279.html
#include "stdafx.h" #include <string> #include <stdlib.h> #include <crtdbg.h> #include <iostream> using namespace std; #ifdef _DEBUG #define new new(_NORMAL_BLOCK, __FILE__, __LINE__) #endif void EnableMemLeakCheck() { int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG); tmpFlag |= _CRTDBG_LEAK_CHECK_DF; _CrtSetDbgFlag(tmpFlag); } int _tmain(int argc, _TCHAR* argv[]) { EnableMemLeakCheck(); //_CrtSetBreakAlloc(207); char* p = new char[100]; //delete p; getchar(); //_CrtDumpMemoryLeaks();//这个代码好像会输出额外多余的内存分配信息 return 0; }
如果动态申请的内存没有释放,调试信息就会显示有内存泄漏