头文件中包括:
#ifdef _DEBUG // 内存泄漏检测支持。
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <malloc.h> // 解决 malloc.h 与 crtdbg.h 顺序导致的 Debug Assertion Failed, "Corrupted pointer passed to _freea" 。
#include <crtdbg.h>
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
int main()
{
_CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_DEBUG );
// 其它代码。
_CrtDumpMemoryLeaks();
return 0;
}
通常会在输出窗口中会有更详细的信息,能够定位到代码行。
如果有多个独立的 DLL,每个 DLL 都要添加。