关于VS2008中C/C++内存泄漏的定位的方法

本文详细介绍了如何使用C++中的_CrtDumpMemoryLeaks函数和Visual Leak Detector工具来检测并定位内存泄漏问题,包括内存分配与释放的正确实践、使用方法及常见错误示例解析。

联系方式:uestc001@gmail.com,欢迎转载,请注明出处:http://blog.csdn.net/uestc001/article/details/8349125 

1、最主要的就是_CrtDumpMemoryLeaks函数,请看MSDN介绍。 

首先,定义一个头文件,用其重定义一下new操作符。

下面就是这个:myMemoryNew.h

  1. #ifndef _MYMEMORYNEW_H  
  2. #define _MYMEMORYNEW_H  
  3.   
  4. #ifdef _DEBUG  
  5. #include <crtdbg.h>  
  6. #define DEBUG_NEW new(_NORMAL_BLOCK ,__FILE__, __LINE__)  
  7. #else  
  8. #define DEBUG_NEW new  
  9. #endif  
  10. #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:

  1. /*************************************** 
  2. *Copyright by 蓝胖子 
  3. *Author : 蓝胖子 
  4. *Email : uestc001@gmail.com 
  5. *Date : 2012.12.20 
  6. *Modefy :2012.12.20 
  7. ***************************************/  
  8. #include <iostream>   
  9. using namespace std;  
  10.   
  11. /*************************************/  
  12. #ifdef _DEBUG   
  13. #include "myMemoryNew.h"   
  14. #define new DEBUG_NEW   
  15. #endif                                                                        
  16. /*************************************/  
  17.   
  18.   
  19. void GetMemory(char **str)  
  20. {  
  21.     *str =new char[10 * sizeof(char)];  
  22. }  
  23.   
  24. int main()  
  25. {  
  26.     char *str = NULL;  
  27.     GetMemory(&str);  
  28.     strcpy(str, "abc");  
  29.     printf("%s\n", str);  
  30.     _CrtDumpMemoryLeaks();//重要语句   
  31.     return 0;  
  32. }  
/***************************************
*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写的测试界面存在也有泄漏,寒!而且泄漏也被检测到了。

如图:

同样用前文的例子如下:

  1. /*************************************** 
  2. *Copyright by 蓝胖子 
  3. *Author : 蓝胖子 
  4. *Email : uestc001@gmail.com 
  5. *Date : 2012.12.20 
  6. *Modefy :2012.12.20 
  7. ***************************************/  
  8. #include <iostream>   
  9. #include "vld.h"   
  10. using namespace std;  
  11.   
  12. void GetMemory(char **str)  
  13. {  
  14.     *str =new char[10 * sizeof(char)];  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     char *str = NULL;  
  20.     GetMemory(&str);  
  21.     strcpy(str, "abc");  
  22.     printf("%s\n", str);  
  23.     delete []str;  
  24.     str = NULL;  
  25.     return 0;  
  26. }  
/***************************************
*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;
}

检测结果:


可以看到,定位准确。

修改加上:

  1. delete []str;  
  2. str = NULL;  
delete []str;
str = NULL;

结果:

vs2008提示窗口的说明:

Call Stack:泄露内存的调用堆栈,显示了泄露资源创建的位置,双击便定位到相应的行。

Data:泄露内存的内容。

总结:推荐使用Visual Leak Detector,好用、免费、准确。

Visual Leak Detector以后,debug下,运行速度明显慢,和Visual Leak Detector机制有关。

 参考:

1、http://blog.csdn.net/hhygcy/article/details/4103155

2、http://blog.csdn.net/akof1314/article/details/7549979
(完)

 

此文章来自于【http://blog.csdn.net/uestc001/article/details/8349125

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值