您的位置:首页 > 编程语言 > C语言/C++

关于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

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: