Using Visual Leak Detector

本文详细介绍了如何在Visual C++ 2008、2010、2012中集成并使用Visual Leak Detector来检测和定位内存泄漏。包括配置步骤、使用方法以及如何查看和解决泄漏报告。

Visual Leak Detector for Visual C++ 2008/2010/2012

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:
For all compiler versions take care to ensure that no junk characters get added when you add the include and library paths. If you browse to the "Program Files(x86) folder using the dialog box provided by Visual Studio and select it you could end up seeing the "%" replacing the "(".

And remember to close and open the Visual Studio IDE once you have modified the default include and library paths which the compiler and linker would always look at.

  • Visual C++ 2010: Go to View ->Property Manager, select Microsoft.Cpp.Win32.user. Select VC++ Directories and then "Include files" from the tree. 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\Win32 subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list. Repeat for Microsoft.Cpp.x64.user, but select lib\Win64 subdirectory instead.
  • Visual C++ 2005 and 2008: 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\Win32 subdirectory from the Visual Leak Detector installation directory. Again, move it to the bottom of the list.
  • Visual C++ 2003: 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\Win32 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\Win32 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.

 

Last edited Oct 29 2011 at 5:18 PM by pramis, version 5

03-19
### Continuous Professional Development (CPD) in IT Continuous Professional Development (CPD) 是指信息技术领域中的专业人士通过持续学习和技能提升来保持其职业竞争力的过程。这通常涉及参加培训课程、研讨会、在线教育平台的学习以及获取新的认证等方法[^3]。 对于软件工程师而言,CPD 可能包括掌握新编程语言、框架和技术栈的知识更新;而对于系统管理员,则可能意味着了解最新的服务器管理技术或安全协议的变化。此外,在实际工作中应用这些新技术也是 CPD 的重要组成部分之一。 ### CodePlex Detector Tool CodePlex 曾经是一个由 Microsoft 提供支持的开源项目托管服务网站,类似于今天的 GitHub 或 Bitbucket。然而需要注意的是,尽管微软已经开放了许多以前封闭的产品和服务,但在早期阶段比如 Roslyn 编译器平台刚起步的时候,它还是属于闭源状态下的产物[^2]。 至于所谓的 “CodePlex Detector Tool”,实际上并不存在这样一个具体命名的技术工具。如果这里指的是某种形式上的代码分析工具的话,那么更接近的概念应该是像 Visual Leak Detector 这样的专门用于检测特定问题(例如内存泄露)的应用程序[^1]。不过如果是关于寻找潜在重复代码片段方面的需求,可以考虑使用 Clone Detective for Visual Studio 插件来进行此类操作。 ```csharp // Example of using a memory leak detection library such as VLD within C++ code. #include <vld.h> // Include the header file from Visual Leak Detector. void SomeFunction() { int* ptr = new int[10]; // Allocate some memory that we forget to free later on purpose. } ``` 上述例子展示了如何在C++项目里集成VLD头文件以便于捕获可能出现却未被释放掉的对象实例情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值