c++检查内存泄漏

时间:2021-08-28 12:44:57

使用_CrtDumpMemoryLeaks()函数检查内存泄漏

 #include <cstdio>
#include <cstdlib>
#include <crtdbg.h> #ifdef _DEBUG //这个要加上,否则不会输出定义到哪个文件中(及不包含存在内存泄露的该cpp文件的相关信息)
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif int main()
{
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
_CrtDumpMemoryLeaks(); int *a = new int[]; //delete a; system("pause");
return ;
}

若存在内存泄漏,在Output信息中会指出内存泄漏的cpp文件。