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

来源:互联网 发布:php用逗号拼接字符串 编辑:程序博客网 时间:2024/06/06 03:44

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

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

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

下面就是这个:myMemoryNew.h

[html] view plaincopyprint?
  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  

 其次,CrtDumpMemoryLeaks()就是显示当前的内存泄漏。注意是“当前”,也就是说当它执行时,所有未销毁的对象均会报内存泄漏。所以,让这条语句在程序的最后执行即main函数的return 0前边最好。
例子1:

[cpp] view plaincopyprint?
  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. }  

 测试结果:

可以看到准确定位:在21行存在泄漏即new了之后没有delete[]和置为NULL。 

2、还有一个Visual Leak Detector[点我下载],相当厉害。请参考博文:Visual Leak Detector 2.2.3 Visual C++内存检测工具 

我在安装配置好了,出现不能正常使用是问题,折腾了一个小时,后来安装了全部vc运行库[点我下载],可正常!

ps:我Qt写的测试界面存在也有泄漏,寒!而且泄漏也被检测到了。

如图:

同样用前文的例子如下:

[cpp] view plaincopyprint?
  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. }  

检测结果:


可以看到,定位准确。

修改加上:

[cpp] view plaincopyprint?
  1. delete []str;  
  2. 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】

原创粉丝点击