(搬运工)内存泄露检测工具 Visual Leak Detector for Visual C++ 2008/2010

本文介绍如何使用VisualLeakDetector工具检测并解决C++应用中的内存泄漏问题,包括安装步骤、代码示例及运行效果解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Visual Leak Detector是一款免费的检查内存泄露的工具。最新的2.1版本可以很好的支持VS2008/2010。

http://www.cppblog.com/ming81/archive/2011/10/31/159381.aspx

点击这里下载

 

安装后,在VS中设置Tool->Options 选择Projects and Solutions中的VC++ Directories,将Visual Leak Detector安装路径中的include和lib路径设置Include Files和Library Files中,如有必要可以将安装路径下bin里的相应dll拷贝至System32下。

创建一个简单的工程,其中指针b所指向的内存没有被正确地释放。

  1. void leak()
  2. {
  3. int *b = new int[10];
  4. for (int i = 0; i < 10; ++i)
  5. {
  6. b[i] = i;
  7. }
  8. }


  9. int main()
  10. {
  11. leak();
  12. return 0;
  13. }

 

include<vld.h>,运行就能在output窗口看到效果啦。

 

输出的部分主要分为两块

Call Stack部分:

是泄露内存的调用堆栈,其中显示了泄露资源创建的位置,双击便可以定位到相应的代码部分。

Data部分:

即使泄露部分的内存内容。

 

注:在自己机器上VS2008上,用了下1.0和1.9b的版本,都出现了不同的问题(1.0版本不能定位泄露代码,1.9b没有正确检查出泄露)。2.1版本运行一切正常。

Project Description

Visual Leak Detector is a free, robust, open-source memory leak detection system for Visual C++.

It's pretty easy to use. After installing it, you just need to tell Visual C++ where to find the included header and library file.

Then it can be used with any C/C++ project simply by adding the following line to your code:
#include <vld.h> 

When you run your program under the Visual Studio debugger, Visual Leak Detector will output a memory leak report at the end of your debugging session. The leak report includes the full call stack showing how any leaked memory blocks were allocated. Double-click on a line in the call stack to jump to that file and line in the editor window.

It's a very effective way to quickly diagnose, and fix, memory leaks in C/C++ applications.

The main difference between the CRT Debug Library and VLDis that Visual Leak Detector shows you the complete callstack used for memory allocation has led to the leak.

For example:

 

---------- Block 1199 at 0x04BE1058: 136 bytes ----------
Call Stack:
d:\Foobar\FooLog.cpp (26): FooLog::getInstance
d:\Foobar\FooMain.cpp (75): FooMain::init
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (578): __tmainCRTStartup
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): WinMainCRTStartup
0x759A3677 (File and line number not available): BaseThreadInitThunk
0x770C9D42 (File and line number not available): RtlInitializeExceptionChain
0x770C9D15 (File and line number not available): RtlInitializeExceptionChain
Data:
9C 33 2D 6B    74 2A 2D 6B    C8 11 BE 04    00 00 00 00     .3-kt*-k ........
00 00 00 00    70 14 BB 6C    70 14 BB 6C    00 00 00 00     ....p..l p..l....
00 00 00 00    68 14 BB 6C    68 14 BB 6C    00 00 00 00     ....h..l h..l....
00 00 00 00    6C 14 BB 6C    6C 14 BB 6C    20 12 BE 04     ....l..l l..l....
00 00 00 00    CD 00 CD CD    00 00 00 00    01 CD CD CD     ........ ........
68 14 BB 6C    78 33 2D 6B    00 00 00 00    00 00 00 00     h..lx3-k ........
00 00 00 00    01 02 00 00    06 00 00 00    00 00 00 00     ........ ........
00 00 00 00    00 00 00 00    88 11 BE 04    5C 10 BE 04     ........ ....\...
00 00 00 00    20 CD CD CD                                   ........ ........

This software is provided "AS IS" without warranty of any kind.

The project was originally developed by Dan Moulding, but they are no longer supported. Features of version 2.0 are implemented by Arkadiy Shapkin (me).


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值