现在的位置: 首页 > 综合 > 正文

VC++ 下的内存泄漏检测工具 Visual Leak Detector

2013年09月14日 ⁄ 综合 ⁄ 共 4046字 ⁄ 字号 评论关闭

下载地址:http://www.codeproject.com/KB/applications/visualleakdetector.aspx

如果你在使用 VC 2008 / 2010, 那么需要下载目前最新的 2.0b , 下载地址:http://vld.codeplex.com/  或

 

http://download.csdn.net/source/3229600

该工具的使用方法很简单,软件文档里有清晰的说明:

Using Visual Leak Detector

This section briefly describes the basics of using Visual Leak Detector (VLD).

Important! : Before using VLD with any Visual C++ project, you must first add the Visual Leak Detector include and library directories to the Visual C++ include and library directory search paths:

    * Visual C++ 8 and 9: Go to Tools -> Options -> Projects and Solutions -> VC++ Directories. Select "Include files" from the "Show Directories For" drop-down menu. Add the include subdirectory from the Visual Leak Detector installation directory. Move it to the bottom of the list. Then select "Library files" from the drop-down menu and add the lib subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list.
    * Visual C++ 7: Go to Project Properties -> C/C++ -> General -> Additional Include Directories and add the include subdirectory from the Visual Leak Detector installation directory. Move it to the bottom of the list. Then select Additional Library Directories and add the lib subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list.
    * Visual C++ 6: Go to Tools -> Options -> Directories. Select "Include files" from the "Show Directories For" drop-down menu. Add the include subdirectory from the Visual Leak Detector installation directory. Move it to the bottom of the list. Then select "Library files" from the drop-down menu and add the lib subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list.

To use VLD with your project, follow these simple steps:

   1. In at least one C/C++ source file from your program, include the vld.h header file. It should not matter which file you add the include statement to. It also should not matter in what order the header is included in relation to other headers. The only exception is stdafx.h (or any other precompiled header). A precompiled header, such as stdafx.h, must always be the first header included in a source file, so vld.h must be included after any precompiled headers.
   2. If your program contains one or more DLLs that you would also like to check for memory leaks, then also include vld.h in at least one source file from each DLL to be included in leak detection.
   3. Build the debug version of your program.

Note: Unlike earlier (pre-1.9) versions of VLD, it is now acceptable to include vld.h in every source file, or to include it in a common header that is included by many or all source files. Only one copy of the VLD code will be loaded into the process, regardless of how many source files include vld.h.

VLD will detect memory leaks in your program whenever you run the debug version. When you run the program under the Visual C++ debugger, a report of all the memory leaks detected will be displayed in the debugger's output window when your program exits (the report can optionally be saved to a file instead, see ReportFile under Configuration Options). Double-clicking on a source file's line number in the memory leak report will take you to that file and line in the editor window, allowing easy navigation of the code path leading up to the allocation that resulted in the memory leak.

Note: When you build release versions of your program, VLD will not be linked into the executable. So it is safe to leave vld.h included in your source files when doing release builds. Doing so will not result in any performance degradation or any other undesirable overhead.

需要注意的是,由于该软件是个开源的工具,并不是成熟的商业产品,所以易用性并不是很好,有一些小问题,还是需要我们自己来动手解决的。以下是我遇到的问题和解决方法:

1、F5 运行程序后,提示"无法定位程序输入点 ???? 于动态链接库 dbghelp.dll 上"

   解决方法:查看[输出]窗口,看看加载的是哪个目录下的 dbghelp.dll 。
             如果发现 “???.exe”: 已加载“C:/WINDOWS/system32/dbghelp.dll” 之类的内容
             那么问题出在这里,程序自动加载了系统目录下的dbghelp.dll ,
             而没有加载 VLD 的dbghelp.dll。
             解决方法:(1)删除系统目录下的dbghelp.dll 或暂时改名。(2)或者把 VLD 安装路径下的 dbghelp.dll拷贝到你的程序的Debug文件夹里。
             总之,只要能够让程序加载VLD的dbghelp.dll就行了。考虑到一般不要动系统目录里的东西,建议采用方法(2)。
             采用方法(2)时,不要把 vld_x86.dll 拷贝到Debug目录下,如果拷了,会使程序堆栈溢出,崩溃。
        
         注:如果vld_x86.dll也不能正常加载,说明没有设置环境变量,重新安装VLD,并且在安装程序询问是否设置环境变量时,选[Yes]
            
2、检测出泄露时,无法显示泄露的位置,无法定位到CPP文件和代码行。并且有大量误报。

   例如:Call Stack:
      0x0041907C (File and line number not available): (Function name unavailable)
 
            
   解决方法:该问题的起因是VLD没能找到.PDB文件,导致产生误报、无法定位代码位置。要解决该问题,就要从怎么让VLD找到.PDB文件入手

             (1)确保VC工程文件所在的目录路径不含中文字符、双字节字符。(VLD对中文路径支持不了)
             (2)查看是否正常生成了.pdb文件。

抱歉!评论已关闭.