内存泄露检测工具[VLD, LeakDiag]

6 篇文章 0 订阅

 

最近将一个指纹匹配算法按照AFIS系统的接口标准做成dll上服务器测试,结果在进行200万人指纹的大库比对时出错了,通过分析发现是dll中存在内存泄露导致系统资源耗尽。虽然一次只有那么200kByte,但乘上200万这个基数也是不小。于是找来了两个工具来检测代码中的内存泄露:VLD和LeakDiag。

1.Visual Leak Detector 1.0 (VLD)

这是一个小巧内存检测工具,是为Visual C++用户设计的。其特色为:

  1. 简单易用,只需要在需要进行内存泄漏检测的的主程序代码中加入#include "VLD.h“,并将编译好的Lib文件放入默认的库目录,然后在Visual C++用Debug模式编译运行程序,程序运行结束后便可在Visual C++的输出窗口看到内存泄露检测结果。
  2. 通过宏定义控制测试行为,如报告的详略等。
  3. 支持Windows X64。
  4. 检测报告很直观,甚至能显示泄漏内存的内容。

下面是一段用于演示VLD功能的代码:

TestVLD.cpp

 

  1. #include "stdafx.h"  
  2. #include "vld.h"  
  3. #include "afx.h"  
  4. #include "string.h"  
  5.   
  6. typedef void(*FARPROC2)(intchar*);  // 用于调用dll中函数的指针  
  7.   
  8. void leak5Bytes();  // 泄漏5个字节内容为"ABCDE"内存  
  9. void leakInDll();   // 调用dll中会造成内存泄漏函数  
  10.   
  11. int _tmain(int argc, _TCHAR* argv[])  
  12. {  
  13.     int * p = new int[10];   
  14.     for (int i=0; i<10; ++i) p[i]=i;  // 泄漏10个int的内存,内容分别为0到9  
  15.     leak5Bytes(); // 泄漏5个字节内存  
  16.     leakInDll();  // 调用dll中会造成内存泄泄漏的函数  
  17.     return 0;  
  18. }  
  19.   
  20. void leak5Bytes()  
  21. {  
  22.     char * p = new char[5];  
  23.     for (int i=0; i<5; ++i) p[i]='A'+i;  // 泄漏的5个字节内存为"ABCDE"  
  24. }  
  25.   
  26. void leakInDll()  
  27. {  
  28.     char *p = new char[16];  
  29.     strcpy(p, "dll leaking!");           // 调用dll函数,在dll中分配一个16字节的空间保存"dll leaking!",且不释放内存。  
  30.     HMODULE hMod = LoadLibrary(_T("Dll.dll"));  
  31.     if (hMod) {  
  32.         FARPROC2 hLeakInDll = (FARPROC2) GetProcAddress(hMod, "dllLeak");  
  33.         hLeakInDll(16, p);  
  34.     }  
  35. }  

 

下面是Dll中被调用的函数代码

 

  1. void dllLeak(int n, char * st)  
  2. {  
  3.     char *p = new char[n];  
  4.     for (int i=0; i<n; ++i) p[i] = st[i];   
  5. }  

 

运行完毕之后的输出为:

 

  1. WARNING: Visual Leak Detector detected memory leaks!  
  2. ---------- Block 57 at 0x01C899D8: 16 bytes ----------  
  3.   Call Stack:  
  4.     f:/rtm/vctools/crt_bld/self_x86/crt/src/new2.cpp (27): operator new[]  
  5.     f:/rtm/vctools/crt_bld/self_x86/crt/src/crt0.c (318): __tmainCRTStartup  
  6.     f:/rtm/vctools/crt_bld/self_x86/crt/src/crt0.c (187): wmainCRTStartup  
  7.     0x7D517D2A (File and line number not available): BaseProcessInitPostImport  
  8.   Data:  
  9.     64 6C 6C 20    6C 65 61 6B    69 6E 67 21    00 CD CD CD     dll.leak ing!....  
  10.   
  11. ---------- Block 56 at 0x01C89990: 5 bytes ----------  
  12.   Call Stack:  
  13.     f:/rtm/vctools/crt_bld/self_x86/crt/src/new2.cpp (27): operator new[]  
  14.     f:/rtm/vctools/crt_bld/self_x86/crt/src/crt0.c (318): __tmainCRTStartup  
  15.     f:/rtm/vctools/crt_bld/self_x86/crt/src/crt0.c (187): wmainCRTStartup  
  16.     0x7D517D2A (File and line number not available): BaseProcessInitPostImport  
  17.   Data:  
  18.     41 42 43 44    45                                            ABCDE... ........  
  19.   
  20. ---------- Block 55 at 0x01C89928: 40 bytes ----------  
  21.   Call Stack:  
  22.     f:/rtm/vctools/crt_bld/self_x86/crt/src/new2.cpp (27): operator new[]  
  23.     f:/rtm/vctools/crt_bld/self_x86/crt/src/crt0.c (318): __tmainCRTStartup  
  24.     f:/rtm/vctools/crt_bld/self_x86/crt/src/crt0.c (187): wmainCRTStartup  
  25.     0x7D517D2A (File and line number not available): BaseProcessInitPostImport  
  26.   Data:  
  27.     00 00 00 00    01 00 00 00    02 00 00 00    03 00 00 00     ........ ........  
  28.     04 00 00 00    05 00 00 00    06 00 00 00    07 00 00 00     ........ ........  
  29.     08 00 00 00    09 00 00 00                                   ........ ........  
  30.   
  31. Visual Leak Detector detected 3 memory leaks.  

 

可以看到,VLD检测出了分别在主程序函数t_main、leak5Bytes以及Dll.dll中dllLeak的内存泄漏,并显示了泄漏内存的内容。

2. LeakDiag

之前的VLD必须和源代码一起编译,若是遇到主程序源码不方便重新编译的情况,则可以选择使用LeakDiag。LeakDiag是微软的一款内存泄漏检测工具,鉴于已经有大牛写了比较详尽的使用说明,我就不再赘述,直接引用大牛的博客地址:

http://www.cppblog.com/sandy/archive/2008/08/18/59260.html

LeakDiag下载地址:

ftp://ftp.microsoft.com/PSS/Tools/Developer%20Support%20Tools/LeakDiag/

 

 

 

 

 

 

工具:

1. IBM   Rational   PurifyPlus是一套完整的运行时分析工具,旨在提高应用程序的可靠性和性能。PurifyPlus将内存错误和泄漏检测、应用程序性能描述、代码覆盖分析等功能组合在一个单一、完整的工具包中。     
  网站:   
  http://www-128.ibm.com/developerworks/rational/products/purifyplus   
    
  从这里可以得到   IBM   Rational   的试用版(DVD),   
  http://www-128.ibm.com/developerworks/cn/offers/sek/index.html   
    
2. BoundsChecker   
  BoundsChecker是一个C++运行时错误检测和调试工具。它通过在Visual   Studio内自动化调试过程加速开发并且缩短上市的周期。BoundsChecker提供清楚,详细的程序错误分析,许多是对C++独有的并且在static,stack和heap内存中检测和诊断错误,以及发现内存和资源的泄漏。   
    
3. Insure++   
  参考站点:http://www.parasoft.com/   
  一个自动化的运行时程序测试工具,检查难以察觉的错误,如内存覆盖,内存泄漏,内存分配错误,变量初始化错误,变量定义冲突,指针错误,库错误,逻辑错误和算法错误等。   
    
4.GlowCode   
  参考站点:http://www.glowcode.com/   
  GlowCode包括内存泄漏检查,code   profiler,函数调用跟踪等功能。给C++开发者提供完整的错误诊断,和运行时性能分析工具包。 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值