在windows下使用 Visual Leak Detector for Visual C++ 2008的安装和配置

来源:互联网 发布:python编译环境 编辑:程序博客网 时间:2024/05/17 04:57

内存泄露工具Visual Leak Detector

在windows下使用

Visual Leak Detector for Visual C++ 2008的安装和配置


1vld官方网站:http://vld.codeplex.com/releases

 

2、下载vld软件,我这边下载vld-2.2.3-setup

 

3、安装配置vld-2.2.3-setup

可以去环境变量里面检查一下,如果没有添加就手动添加下(安装程序默认自动添加)。


一直点next。

 

接下来就是配置了,需要在VS2008中Tools->Options->Projects andSolutions->VC Directories 中设置VLD的引用文件路径和库文件路径。


 

 

4、使用:

测试程序:

#include<vld.h> //在包含入口函数的.cpp文件中包含vld.h就可以。如果这个cpp文件包含了stdafx.h,则将包含vld.h的语句放在stdafx.h的包含语句之后,否则放在最前面。

#include <stdlib.h>

#include <stdio.h>

 

void f()

{

    int*p = new int(0x12345678);

    printf("p=%08x,", p);

}

int main()

{

    f();

    return0;

}

 

如果工程不是用visual studio2008建的,还需要进行如下操作(vs2008在配置时已经添加了引用文件路径和库文件路径):

1、  在编译之前,需要做这样一步(64位的程序操作类似):拷贝lib文件


2、  拷贝dll文件


如果拷贝错误的dll(32位的程序拷贝64位的dll)或者没有拷贝,会出现如下错误应用程序正常初始化 0xc015002 失败:

 

 

 

5、结果:

Visual Leak Detector打印信息:

WARNING: Visual Leak Detector detected memory leaks!

---------- Block 1 at 0x00594F80: 4 bytes ---------- --1号块0x00594F80地址泄漏了4个字节

  Call Stack:                                        --下面是调用堆栈

   c:\users\administrator\documents\visual studio2008\projects\test_leak\test_leak\test_leak.cpp (7): test_leak.exe!f + 0x7bytes--表示在test_leak.cpp7行的f()函数

   c:\users\administrator\documents\visual studio2008\projects\test_leak\test_leak\test_leak.cpp (13): test_leak.exe!main–双击可以引导至对应代码处

   f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (582):test_leak.exe!__tmainCRTStartup + 0x19 bytes

   f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (399): test_leak.exe!mainCRTStartup

    0x74A0336A (File and line numbernot available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes

    0x76FF9F72 (File and line numbernot available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes

    0x76FF9F45 (File and line numbernot available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes

  Data:                                            --这是泄漏内存的内容,0x12345678

    78 56 34 12                                                 xV4..... ........

 

 

Visual Leak Detector detected 1 memory leak (40 bytes).

Largest number used: 40 bytes.

Total allocations: 40 bytes.

Visual Leak Detector is now exiting.

The program '[8020] test_leak.exe: Native' has exited with code 0 (0x0).

 

 

程序没有内存泄露的打印信息:

Visual Leak Detector Version 2.2.3 installed.

No memory leaks detected.

VisualLeak Detector is now exiting.

 

6、结束语:

这是一个很方便易用的工具,安装后每次使用时,仅仅需要将它头文件包含进来重新build就可以。而且,该工具仅在buildDebug版的时候会连接到你的程序中,如果build Release版,该工具不会对你的程序产生任何性能等方面影响。所以尽可以将其头文件一直包含在你的源代码中。

这里顺便提一下,vld能够检测以下问题:

1可以得到内存泄漏点的调用堆栈,如果可以的话,还可以得到其所在文件及行号;
2
可以得到泄露内存的完整数据;
3
可以设置内存泄露报告的级别;
4
它是一个已经打包的lib,使用时无须编译它的源代码。而对于使用者自己的代码,也只需要做很小的改动(添加头文件vld.h);
5
他的源代码使用GNU许可发布,并有详尽的文档及注释。对于想深入了解堆内存管理的读者,是一个不错的选择。

0 0
原创粉丝点击