VS检查内存泄露的方法

_CrtDumpMemoryLeaks函数

系统自带的 C C C R u n − T i m e ( C R T ) Run-Time (CRT) Run−Time(CRT)库可以帮助我们检测内存泄露

#include <iostream>
using namespace std;

#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#define new DEBUG_CLIENTBLOCK
#endif 

void GetMemoryLeak()
{
          
   
	_CrtDumpMemoryLeaks();
}

int main()
{
          
   
	int* p = new int[10];
	//delete p;
	GetMemoryLeak();
	cin.get();
	return 0;
}

对比释放之后

#include <iostream>
using namespace std;

#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#define new DEBUG_CLIENTBLOCK
#endif 

void GetMemoryLeak()
{
          
   
	_CrtDumpMemoryLeaks();
}

int main()
{
          
   
	int* p = new int[10];
	delete p;
	GetMemoryLeak();
	cin.get();
	return 0;
}

Visual Leak Detector(VLD)插件

给VS装上该插件,包含头文件#include <vld.h>

VS2015内存快照

VS2015及以上版本调用内存快照

经验分享 程序员 微信小程序 职场和发展