在主程序.h文件加上
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
在main函数第一行加上
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
检测结果如图:
对应在main函数中添加
_CrtSetBreakAlloc(315);
运行会在内存泄露出发生中断
(可能会误判全局变量,解决问题时注意观察)
参考自: