C++内存泄漏检测工具

在程序中增加相应的内存检测工具 

#define CRTDBG MAP ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#ifdef DEBUG
#ifndef DBGNEW
#define DBG_NEW new (_NORMAL_BLOCK,_FILE_LINE_)
#define new DBG NEW
#endif
#endif

_CrtDumpMemoryLeaks();

当没有释放内存时候:

#define _CRT_SECURE_NO_WARNINGS

#define CRTDBG MAP ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#include<iostream>
#include<stdio.h>
#include<Windows.h>




#ifdef DEBUG
#ifndef DBGNEW
#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();
		system("pause");
		return 0;
	}

}

增加了delete时候:

#define _CRT_SECURE_NO_WARNINGS

#define CRTDBG MAP ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#include<iostream>
#include<stdio.h>
#include<Windows.h>




#ifdef DEBUG
#ifndef DBGNEW
#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();
		system("pause");
		return 0;
	}

}

 

相关推荐

  1. C++ 内存泄漏检测工具——Valgrind(Linux系统)

    2024-01-25 02:52:01       35 阅读
  2. go语言内存泄漏检查工具

    2024-01-25 02:52:01       47 阅读
  3. C++中实现一个泄漏检测工具

    2024-01-25 02:52:01       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-25 02:52:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-25 02:52:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-25 02:52:01       82 阅读
  4. Python语言-面向对象

    2024-01-25 02:52:01       91 阅读

热门阅读

  1. 分布式事务理论基础

    2024-01-25 02:52:01       57 阅读
  2. 03 | 事务隔离

    2024-01-25 02:52:01       50 阅读
  3. vant组件库的简单使用

    2024-01-25 02:52:01       63 阅读
  4. [Oracle] INSERT INTO 几种用法

    2024-01-25 02:52:01       61 阅读
  5. 【无标题】KADB使用DBLINK连接KES验证

    2024-01-25 02:52:01       62 阅读
  6. Git学习笔记:2 实战技巧

    2024-01-25 02:52:01       55 阅读
  7. LeetCode刷题笔记之二叉树(一)

    2024-01-25 02:52:01       53 阅读