关于VS2008中C/C++内存泄漏的定位的方法
2013-01-22 16:35
316 查看
联系方式:uestc001@gmail.com,欢迎转载,请注明出处:http://blog.csdn.net/uestc001/article/details/8349125
1、最主要的就是_CrtDumpMemoryLeaks函数,请看MSDN介绍。
首先,定义一个头文件,用其重定义一下new操作符。
下面就是这个:myMemoryNew.h
[html]
view plaincopyprint?
#ifndef _MYMEMORYNEW_H #define _MYMEMORYNEW_H #ifdef _DEBUG #include <crtdbg.h> #define DEBUG_NEW new(_NORMAL_BLOCK ,__FILE__, __LINE__) #else #define DEBUG_NEW new #endif #endif //_MYMEMORYNEW_H
其次,CrtDumpMemoryLeaks()就是显示当前的内存泄漏。注意是“当前”,也就是说当它执行时,所有未销毁的对象均会报内存泄漏。所以,让这条语句在程序的最后执行即main函数的return 0前边最好。
例子1:
[cpp]
view plaincopyprint?
/***************************************
*Copyright by 蓝胖子
*Author : 蓝胖子
*Email : uestc001@gmail.com
*Date : 2012.12.20
*Modefy :2012.12.20
***************************************/
#include <iostream>
using namespace std;
/*************************************/
#ifdef _DEBUG
#include "myMemoryNew.h"
#define new DEBUG_NEW
#endif
/*************************************/
void GetMemory(char **str)
{
*str =new char[10 * sizeof(char)];
}
int main()
{
char *str = NULL;
GetMemory(&str);
strcpy(str, "abc");
printf("%s\n", str);
_CrtDumpMemoryLeaks();//重要语句
return 0;
}
测试结果:
可以看到准确定位:在21行存在泄漏即new了之后没有delete[]和置为NULL。
2、还有一个Visual Leak Detector[点我下载],相当厉害。请参考博文:Visual
Leak Detector 2.2.3 Visual C++内存检测工具
我在安装配置好了,出现不能正常使用是问题,折腾了一个小时,后来安装了全部vc运行库[点我下载],可正常!
ps:我Qt写的测试界面存在也有泄漏,寒!而且泄漏也被检测到了。
如图:
同样用前文的例子如下:
[cpp]
view plaincopyprint?
/*************************************** *Copyright by 蓝胖子 *Author : 蓝胖子 *Email : uestc001@gmail.com *Date : 2012.12.20 *Modefy :2012.12.20 ***************************************/ #include <iostream> #include "vld.h" using namespace std; void GetMemory(char **str) { *str =new char[10 * sizeof(char)]; } int main() { char *str = NULL; GetMemory(&str); strcpy(str, "abc"); printf("%s\n", str); delete []str; str = NULL; return 0; }
检测结果:
可以看到,定位准确。
修改加上:
[cpp]
view plaincopyprint?
delete []str; str = NULL;
结果:
vs2008提示窗口的说明:
Call Stack:泄露内存的调用堆栈,显示了泄露资源创建的位置,双击便定位到相应的行。
Data:泄露内存的内容。
总结:推荐使用Visual Leak Detector,好用、免费、准确。
Visual Leak Detector以后,debug下,运行速度明显慢,和Visual Leak Detector机制有关。
参考:
1、/article/8206096.html
2、/article/1899514.html
(完)
此文章来自于【http://blog.csdn.net/uestc001/article/details/8349125】
1、最主要的就是_CrtDumpMemoryLeaks函数,请看MSDN介绍。
首先,定义一个头文件,用其重定义一下new操作符。
下面就是这个:myMemoryNew.h
[html]
view plaincopyprint?
#ifndef _MYMEMORYNEW_H #define _MYMEMORYNEW_H #ifdef _DEBUG #include <crtdbg.h> #define DEBUG_NEW new(_NORMAL_BLOCK ,__FILE__, __LINE__) #else #define DEBUG_NEW new #endif #endif //_MYMEMORYNEW_H
#ifndef _MYMEMORYNEW_H #define _MYMEMORYNEW_H #ifdef _DEBUG #include <crtdbg.h> #define DEBUG_NEW new(_NORMAL_BLOCK ,__FILE__, __LINE__) #else #define DEBUG_NEW new #endif #endif //_MYMEMORYNEW_H
其次,CrtDumpMemoryLeaks()就是显示当前的内存泄漏。注意是“当前”,也就是说当它执行时,所有未销毁的对象均会报内存泄漏。所以,让这条语句在程序的最后执行即main函数的return 0前边最好。
例子1:
[cpp]
view plaincopyprint?
/***************************************
*Copyright by 蓝胖子
*Author : 蓝胖子
*Email : uestc001@gmail.com
*Date : 2012.12.20
*Modefy :2012.12.20
***************************************/
#include <iostream>
using namespace std;
/*************************************/
#ifdef _DEBUG
#include "myMemoryNew.h"
#define new DEBUG_NEW
#endif
/*************************************/
void GetMemory(char **str)
{
*str =new char[10 * sizeof(char)];
}
int main()
{
char *str = NULL;
GetMemory(&str);
strcpy(str, "abc");
printf("%s\n", str);
_CrtDumpMemoryLeaks();//重要语句
return 0;
}
/*************************************** *Copyright by 蓝胖子 *Author : 蓝胖子 *Email : uestc001@gmail.com *Date : 2012.12.20 *Modefy :2012.12.20 ***************************************/ #include <iostream> using namespace std; /*************************************/ #ifdef _DEBUG #include "myMemoryNew.h" #define new DEBUG_NEW #endif /*************************************/ void GetMemory(char **str) { *str =new char[10 * sizeof(char)]; } int main() { char *str = NULL; GetMemory(&str); strcpy(str, "abc"); printf("%s\n", str); _CrtDumpMemoryLeaks();//重要语句 return 0; }
测试结果:
可以看到准确定位:在21行存在泄漏即new了之后没有delete[]和置为NULL。
2、还有一个Visual Leak Detector[点我下载],相当厉害。请参考博文:Visual
Leak Detector 2.2.3 Visual C++内存检测工具
我在安装配置好了,出现不能正常使用是问题,折腾了一个小时,后来安装了全部vc运行库[点我下载],可正常!
ps:我Qt写的测试界面存在也有泄漏,寒!而且泄漏也被检测到了。
如图:
同样用前文的例子如下:
[cpp]
view plaincopyprint?
/*************************************** *Copyright by 蓝胖子 *Author : 蓝胖子 *Email : uestc001@gmail.com *Date : 2012.12.20 *Modefy :2012.12.20 ***************************************/ #include <iostream> #include "vld.h" using namespace std; void GetMemory(char **str) { *str =new char[10 * sizeof(char)]; } int main() { char *str = NULL; GetMemory(&str); strcpy(str, "abc"); printf("%s\n", str); delete []str; str = NULL; return 0; }
/*************************************** *Copyright by 蓝胖子 *Author : 蓝胖子 *Email : uestc001@gmail.com *Date : 2012.12.20 *Modefy :2012.12.20 ***************************************/ #include <iostream> #include "vld.h" using namespace std; void GetMemory(char **str) { *str =new char[10 * sizeof(char)]; } int main() { char *str = NULL; GetMemory(&str); strcpy(str, "abc"); printf("%s\n", str); delete []str; str = NULL; return 0; }
检测结果:
可以看到,定位准确。
修改加上:
[cpp]
view plaincopyprint?
delete []str; str = NULL;
delete []str; str = NULL;
结果:
vs2008提示窗口的说明:
Call Stack:泄露内存的调用堆栈,显示了泄露资源创建的位置,双击便定位到相应的行。
Data:泄露内存的内容。
总结:推荐使用Visual Leak Detector,好用、免费、准确。
Visual Leak Detector以后,debug下,运行速度明显慢,和Visual Leak Detector机制有关。
参考:
1、/article/8206096.html
2、/article/1899514.html
(完)
此文章来自于【http://blog.csdn.net/uestc001/article/details/8349125】
相关文章推荐
- C++程序内存泄漏检测方法
- 关于C和C++混合编译出错的解决方法
- C++中关于数据小数点,取整的方法
- 关于C++和C#中的静态方法的问题!
- 关于PE病毒编写的学习(八)——定位API的N种方法(2010年10月25更新)
- 关于C++中vector定义多维数组的方法
- 【软件技巧】关于mindmanager 2012安装后报C++错误的解决方法
- cocos2dx 关于lua 绑定的环境配置官方文档翻译与 将自定义c++方法绑定到lua的的方法
- C++关于子类调用父类的构造方法的问题
- 关于Linux系统下内存泄漏的检查方法
- c++中关于SQLite中文乱码的解决方法
- 关于vs2008的Eval方法
- C++ 关于 CMFCPropertyGridCtrl 的使用方法 之二 (原创)
- 关于连接SQL 2005 出错: provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错 的解决方法
- 关于在VS2008和VS2010中禁用及卸载Visual Assist X的方法研究
- 关于 TerminateProcess函数 在c++的进程中的学习而发现VS2008的编译安全问题
- 关于百度地图api在WiFi下可以定位在3G网时不能定位解决方法
- C++ 关于 CMFCPropertyGridCtrl 的使用方法
- 在 Linux 平台中调试 C/C++ 内存泄漏方法
- C++ 打开exe文件的方法(VS2008)