在main函数最后面一行,加上一句_CrtDumpMemoryLeaks()。调试程序,自然关闭程序让其退出(不要定制调试),查看输出
头文件:#include <crtdbg.h>
检查内存泄漏,但有时不好用,不能输出文件和行数,简单的文件结构可以
#ifdef _DEBUG #define _CRTDBG_MAP_ALLOC #include "crtdbg.h" #define MYDEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__) #define new MYDEBUG_NEW #endif void main() { char *p1; p1 = new char[40]; _CrtMemDumpAllObjectsSince(NULL); _CrtDumpMemoryLeaks(); }
参考:https://blog.csdn.net/mfcing/article/details/42673393