您的位置:首页 > 其它

定位程序内存泄露的位置

2013-07-26 16:35 806 查看
转自:http://baitai.iteye.com/blog/1020355

1. 头文件加入:

#include <windows.h>

#define _CRTDBG_MAP_ALLOC

#define USING_LEAK_CHECK    1  

#ifndef _crtdbg_map_alloc  

#define _crtdbg_map_alloc  

#endif   

#include <stdlib.h>  

#if USING_LEAK_CHECK  

#include <crtdbg.h>  

#endif  

#include <stdio.h>

具体需要测试的地方插入

 #if USING_LEAK_CHECK

  _CrtDumpMemoryLeaks();  

 #endif

打印此处之前的内存申请情况

 

2. .cpp文件中加入:

#ifdef _DEBUG

#define new   new(_NORMAL_BLOCK, __FILE__, __LINE__)

#endif

可以看到new分配内存情况。

在网上找了很久,都没有成功输出程序内存泄露的位置。后来按照这个写,一开始也没有成功,老是报如下错误:

error C2661: “CObject::operator new” : 没有重载函数接受 4 个参数

后来在http://bbs.csdn.net/topics/200016093这篇帖子的启发下,

将 #define new   new(_NORMAL_BLOCK, __FILE__, __LINE__)

改成 #define
new   DEBUG_NEW


就可以了

thank god!

对于如何定位程序内存泄露的位置,很多人推荐BoundsChecker这个工具,没有用过,先留下记号,有时间研究研究。

http://blog.csdn.net/cwzmb/article/details/3506902
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息