C++中基于Crt的内存泄漏检测(重载new和delete,记录在Map里)
尽管这个概念已经让人说滥了 ,还是想简单记录一下, 以备以后查询。
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
int _tmain(int argc, _TCHAR* argv[])
{
char* p = new char();
char* pp = new char[10];
char* ppp = (char*)malloc(10);
_CrtDumpMemoryLeaks();
return 0;
}
主要原理是运用Crt 的内存调试功能, 通过宏替代默认的operator new, 让它被下面版本替代:
size_t cb,
int nBlockUse,
const char * szFileName,
int nLine
)
_THROW1(_STD bad_alloc)
{
/* _nh_malloc_dbg already calls _heap_alloc_dbg in a loop and calls _callnewh
if the allocation fails. If _callnewh returns (very likely because no
new handlers have been installed by the user), _nh_malloc_dbg returns NULL.
*/
void *res = _nh_malloc_dbg( cb, 1, nBlockUse, szFileName, nLine );
RTCCALLBACK(_RTC_Allocate_hook, (res, cb, 0));
/* if the allocation fails, we throw std::bad_alloc */
if (res == 0)
{
static const std::bad_alloc nomem;
_RAISE(nomem);
}
return res;
}
这样Crt会把此次分配内存的文件名和行号以及大小等记录下来,最后当调用用_CrtDumpMemoryLeaks(); 时如果还没释放就会打印出来。
结果如下:
Dumping objects ->
f:\test\memleakchecker\memleakchecker\memleakchecker.cpp(23) : {108} normal block at 0x0003A1A8, 10 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD
f:\test\memleakchecker\memleakchecker\memleakchecker.cpp(22) : {107} client block at 0x0003A160, subtype 0, 10 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD
f:\test\memleakchecker\memleakchecker\memleakchecker.cpp(21) : {106} client block at 0x0003A120, subtype 0, 1 bytes long.
Data: < > 00
Object dump complete.
下面是一些注意事项:
(1) #define _CRTDBG_MAP_ALLOC 的作用
如果不定义这个宏, C方式的malloc泄露不会被记录下来。
(2)数字{108} {107}的作用
表示第几次分配, 你可以通过_CrtSetBreakAlloc程序运行到预定次数时暂停 ,比如
{
_CrtSetBreakAlloc(108);
char* p = new char();
char* pp = new char[10];
char* ppp = (char*)malloc(10);
_CrtDumpMemoryLeaks();
return 0;
}
(3)如果程序有多个出口或是有涉及到全局变量, 可以通过_CrtSetDbgFlag 设置标志让程序退出时自动打印泄露 , 比如
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
char* p = new char();
char* pp = new char[10];
char* ppp = (char*)malloc(10);
return 0;
}
(4)我们知道宏替代是最粗暴的方式, 所以尽量把下面new的替代宏放到每个Cpp里而不是放到一个通用的头文件中, 实际上MFC也是这么做的
#define new DEBUG_CLIENTBLOCK
#endif
(5)上面的operator new只能照顾到最普通的new, 实际上operator new是有任意多种重载方式, 只需要确保第一个参数是表示大小。 比如下面的placement new就会编译失败, 因为宏替代后格式不符合要求了, 所以如果你的CPP用了非标准的new, 就不要加入new的检测宏了。
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
int _tmain(int argc, _TCHAR* argv[])
{
_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
char* p = new char();
char* pp = new char[10];
char* ppp = (char*)malloc(10);
char d;
char* p1 = new(&d) char('a');
return 0;
}
(6)因为STL里map内的tree用到了placement new, 所以如果你这样用会编译失败:
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
#include <map>
你应该把 #include <map>放到 宏定义的前面。
(7) 如果你在宏 #define new DEBUG_CLIENTBLOCK 之后再声明或定义 operator new函数, 都会因为宏替代而编译失败。
而STL的xdebug文件恰恰申明了operator new函数, 所以请确保new的替代宏放在所有include头文件的最后, 尤其要放在STL头文件的后面。
#include "myclass.h"
#include <map>
#include <algorithm>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
MyClass::MyClass()
{
char* p = new char('a');
}
(8)如果你觉得上面的这种new替代宏分散在各个CPP里太麻烦, 想把所有的东西放到一个通用头文件里,请参考下面定义的方式:
#include <map>
#include <algorithm>
//other STL file

#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif
(9)简单判断某个独立函数有没有内存泄露可以用下面的方法:
{
_CrtMemState m_checkpoint;
public:
explicit DbgMemLeak()
{
_CrtMemCheckpoint(&m_checkpoint);
};
~DbgMemLeak()
{
_CrtMemState checkpoint;
_CrtMemCheckpoint(&checkpoint);
_CrtMemState diff;
_CrtMemDifference(&diff, &m_checkpoint, &checkpoint);
_CrtMemDumpStatistics(&diff);
_CrtMemDumpAllObjectsSince(&diff);
};
};
int _tmain(int argc, _TCHAR* argv[])
{
DbgMemLeak check;
{
char* p = new char();
char* pp = new char[10];
char* ppp = (char*)malloc(10);
}
return 0;
}
http://www.cppblog.com/weiym/archive/2013/02/25/198072.html
C++中基于Crt的内存泄漏检测(重载new和delete,记录在Map里)的更多相关文章
- 基于WinDbg的内存泄漏分析
在前面C++中基于Crt的内存泄漏检测一文中提到的方法已经可以解决我们的大部分内存泄露问题了,但是该方法是有前提的,那就是一定要有源代码,而且还只能是Debug版本调试模式下.实际上很多时候我们的程序 ...
- Cocos开发中性能优化工具介绍之Visual Studio内存泄漏检测工具——Visual Leak Detector
那么在Windows下有什么好的内存泄漏检测工具呢?微软提供Visual Studio开发工具本身没有什么太好的内存泄漏检测功能,我们可以使用第三方工具Visual Leak Detector(以下简 ...
- Android 性能优化之内存泄漏检测以及内存优化(中)
https://blog.csdn.net/self_study/article/details/66969064 上篇博客我们写到了 Java/Android 内存的分配以及相关 GC 的详细分析, ...
- C++程序内存泄漏检测方法
一.前言 在Linux平台上有valgrind可以非常方便的帮助我们定位内存泄漏,因为Linux在开发领域的使用场景大多是跑服务器,再加上它的开源属性,相对而言,处理问题容易形成“统一”的标准.而在W ...
- 利用Android Studio、MAT对Android进行内存泄漏检测
利用Android Studio.MAT对Android进行内存泄漏检测 Android开发中难免会遇到各种内存泄漏,如果不及时发现处理,会导致出现内存越用越大,可能会因为内存泄漏导致出现各种奇怪的c ...
- VS2005内存泄漏检测方法[转载]
一.非MFC程序可以用以下方法检测内存泄露: 1. 程序开始包含如下定义: #ifdef _DEBUG #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __ ...
- 使用CRT定位内存泄漏
1. 使能内存泄漏检测#define _CRTDBG_MAP_ALLOC#include <stdlib.h>#include <crtdbg.h>注1:语句顺序不能修改:注2 ...
- C++内存泄漏检测工具
C++内存泄漏检测工具 1.VC自带的CRT:_CrtCheckMemory 调试器和 CRT 调试堆函数 1.1用法: /************************************ ...
- C的内存泄漏检测
一,Windows平台下的内存泄漏检测 检测是否存在内存泄漏问题 Windows平台下面Visual Studio 调试器和 C 运行时 (CRT) 库为我们提供了检测和识别内存泄漏的有效方法,原理大 ...
随机推荐
- python:时间处理模块
# coding=utf-8 from datetime import date, datetime import time def date_test(): print 'date.max', da ...
- jQuery.isEmptyObject()函数用于判断指定参数是否是一个空对象。
jquery中有一个函数isEmptyObject()用来判断制定参数是否是一个空对象. 示例如下: function isEmptyObject(e) { var t; for (t in e) r ...
- ASP.NET MVC3调用分部视图-PartialView的几种方式(集)
我们的网页通常会有好几个部分组成,一些公共的区域我们通常会做成PartialView(部分视图),但这些视图并不是完全固定的,所以还是需要 特定的Controller来控制.如果只是静态的html,虽 ...
- android开发4:Android布局管理器1(线性布局,相对布局RelativeLayout-案例)
控件类概述 View 可视化控件的基类 属性名称 对应方法 描述 android:background setBackgroundResource(int) 设置背景 android:clickabl ...
- [Angular 2] Using ngrx/store and Reducers for Angular 2 Application State
ngrx/store is a library that simplifies common RxJS patterns for managing state and gives you an eas ...
- 窥探 Swift 之 函数与闭包的应用实例
今天的博客算是比较基础的,还是那句话,基础这东西在什么时候 都是最重要的.说到函数,只要是写过程序就肯定知道函数是怎么回事,今天就来讨论一下Swift中的函数的特性以及Swift中的闭包.今天的一些小 ...
- C#&Sql获取中文字符拼音首字母的方法
C#获取字符拼音首字母,可以存储在数据库中以备将来按字母搜索的需求. public static string GetAc(string s) { try { string temp = Servic ...
- SSH端口修改
打开SSDH配置文件: vim /etc/ssh/sshd_config 添加端口号:Port 60000 重启服务:service sshd restart
- NYOJ 116士兵杀敌(二) 树状数组
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=116 士兵杀敌(一) 数组是固定的,所以可以用一个sum数组来保存每个元素的和就行,但是不 ...
- spring03autowire属性
1.创建需要的实体类 public class Student { //学生实体类 private String name; //姓名 private Integer age; //年龄 privat ...