//第一步,包括以下头文件
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>
#include <Windows.h>
//第二步,接管new操作符,提供追踪内存泄漏功能
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new (_NORMAL_BLOCK,__FILE__,__LINE__)
#define new DBG_NEW
#endif
#endif
using namespace std;
void A_live() {
int* p = new int[1024];
p[0] = 0;
delete[]p;
}
int main() {
for (int i = 0; i < 5; i++)
{
A_live();
Sleep(50);
}
//第三步,输出内存泄漏信息
_CrtDumpMemoryLeaks();
return 0;
}
C++内存泄漏检测
最新推荐文章于 2024-06-09 20:18:37 发布