#include "stdafx.h"
//在main函数所在文件,#include "stdafx.h"下面,添加如下代码:
/
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif // _DEBUG
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif // _DEBUG
在main函数的入口处调用如下代码:
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
......
}
然后,F5调试运行,执行完后,如果有内存泄露,Visual Studio就会报告上来:
Detected memory leaks!
Dumping objects ->
d:\projects\memoryleaktest\memoryleaktest.cpp(22) : {110} client block at 0x003AA148, 20 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
程序“[4628] MemoryLeakTest.exe: 本机”已退出,返回值为0 (0x0)。