VLD调试介绍

来源:互联网 发布:ora27102 windows 编辑:程序博客网 时间:2024/05/16 16:24

VLD调试介绍

Visual Leak Detector是一款用于Visual C++的免费的内存泄露检测工具,相比较其它的内存泄露检测工具,它在检测到内存泄漏的同时,还具有如下特点:

1、  可以得到内存泄漏点的调用堆栈,如果可以的话,还可以得到其所在文件及行号;

2、  可以得到泄露内存的完整数据;

3、  可以设置内存泄露报告的级别;

4、  它是一个已经打包的lib,使用时无须编译它的源代码。

5、  他的源代码使用GNU许可发布,并有详尽的文档及注释。对于想深入了解堆内存管理的读者,是一个不错的选择。

可见,从使用角度来讲,Visual LeakDetector简单易用,对于使用者自己的代码,唯一的修改是#includeVisual Leak Detector的头文件后正常运行自己的程序,就可以发现内存问题。从研究的角度来讲,如果深入Visual Leak Detector源代码,可以学习到堆内存分配与释放的原理、内存泄漏检测的原理及内存操作的常用技巧等。

首先将介绍Visual Leak Detector的使用方法与步骤,

解压VLD之后得到vld.h, vldapi.h, vld.lib, vldmt.lib, vldmtdll.lib, dbghelp.dll等文件。将.h文件拷贝到Visual C++的默认include目录下,将.lib文件拷贝到Visual C++的默认lib目录下,便安装完成了。因为版本问题,如果使用windows 2000或者以前的版本,需要将dbghelp.dll拷贝到你的程序的运行目录下,或其他可以引用到的目录。

接下来需要将其加入到自己的代码中。方法很简单,只要在包含入口函数的.cpp文件中包含vld.h就可以。如果这个cpp文件包含了stdafx.h,则将包含vld.h的语句放在stdafx.h的包含语句之后,否则放在最前面。如下是一个示例程序:

#include "stdafx.h"

#include <vld.h>

#include <stdlib.h>

#include <malloc.h>

#include <vector>

using namespace std;

class newI

{

public:

 newI()  //默认构造函数

 {

      pInt=new int(1);

 }

 newI(const newI& newI_copy)  //复制构造函数

 {

 pInt=new int(*newI_copy.pInt);

 }

 newI(int a)

 {

 pInt=new int(a);

 } 

 ~newI()  //析构

 {

 delete pInt;

 }

private:

 int*pInt;

};

int _tmain(int argc, _TCHAR* argv[])

{    

 vector<newI> *p=newvector<newI>(10);  //泄漏了12次内存都可查出,双击错误信息就能定位到内存泄漏的代码所在,这只是测试例子,实际中new一个容器或向量没什么意义,因为它们自己就可以对内存进行管理。

 return 0;

}

 

编译运行后,在Output窗口得到:

(因为泄漏多处所以提示很长,你可以不看,只要知道双击提示可以定位泄漏的代码就行了)

Visual Leak Detector Version 1.0 installed(multithreaded DLL).

WARNING: Visual Leak Detector detectedmemory leaks!

---------- Block 185 at 0x003ACEE0: 4 bytes----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (18): newI::newI

   f:\vs2005\vc\include\xmemory (53): std::_Construct<newI,newI>

   f:\vs2005\vc\include\xmemory (156): std::allocator<newI>::construct

   f:\vs2005\vc\include\memory (401): std::_Uninit_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

   f:\vs2005\vc\include\memory (916):stdext::unchecked_uninitialized_fill_n<newI *,unsignedint,newI,std::allocator<newI> >

   f:\vs2005\vc\include\vector (1208):std::vector<newI,std::allocator<newI> >::_Ufill

   f:\vs2005\vc\include\vector (536):std::vector<newI,std::allocator<newI> >::_Construct_n

   f:\vs2005\vc\include\vector (470):std::vector<newI,std::allocator<newI> >::vector<newI,std::allocator<newI>>

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   01 00 00 00                                                 ........ ........

 

---------- Block 184 at 0x003ACEA0: 4 bytes----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (18): newI::newI

   f:\vs2005\vc\include\xmemory (53): std::_Construct<newI,newI>

   f:\vs2005\vc\include\xmemory (156):std::allocator<newI>::construct

   f:\vs2005\vc\include\memory (401): std::_Uninit_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

   f:\vs2005\vc\include\memory (916):stdext::unchecked_uninitialized_fill_n<newI *,unsignedint,newI,std::allocator<newI> >

   f:\vs2005\vc\include\vector (1208):std::vector<newI,std::allocator<newI> >::_Ufill

   f:\vs2005\vc\include\vector (536):std::vector<newI,std::allocator<newI> >::_Construct_n

   f:\vs2005\vc\include\vector (470):std::vector<newI,std::allocator<newI>>::vector<newI,std::allocator<newI> >

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

    f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c(594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   01 00 00 00                                                  ................

 

---------- Block 183 at 0x003ACE60: 4 bytes----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (18): newI::newI

   f:\vs2005\vc\include\xmemory (53): std::_Construct<newI,newI>

   f:\vs2005\vc\include\xmemory (156):std::allocator<newI>::construct

   f:\vs2005\vc\include\memory (401): std::_Uninit_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

   f:\vs2005\vc\include\memory (916): stdext::unchecked_uninitialized_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

   f:\vs2005\vc\include\vector (1208):std::vector<newI,std::allocator<newI> >::_Ufill

   f:\vs2005\vc\include\vector (536):std::vector<newI,std::allocator<newI> >::_Construct_n

   f:\vs2005\vc\include\vector (470):std::vector<newI,std::allocator<newI>>::vector<newI,std::allocator<newI> >

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   01 00 00 00                                                 ........ ........

 

---------- Block 182 at 0x003ACE20: 4 bytes----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (18): newI::newI

   f:\vs2005\vc\include\xmemory (53): std::_Construct<newI,newI>

   f:\vs2005\vc\include\xmemory (156):std::allocator<newI>::construct

   f:\vs2005\vc\include\memory (401): std::_Uninit_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

   f:\vs2005\vc\include\memory (916):stdext::unchecked_uninitialized_fill_n<newI *,unsignedint,newI,std::allocator<newI> >

   f:\vs2005\vc\include\vector (1208): std::vector<newI,std::allocator<newI>>::_Ufill

   f:\vs2005\vc\include\vector (536):std::vector<newI,std::allocator<newI> >::_Construct_n

   f:\vs2005\vc\include\vector (470):std::vector<newI,std::allocator<newI>>::vector<newI,std::allocator<newI> >

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   01 00 00 00                                                 ........ ........

 

---------- Block 181 at 0x003ACDE0: 4 bytes----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (18): newI::newI

   f:\vs2005\vc\include\xmemory (53): std::_Construct<newI,newI>

   f:\vs2005\vc\include\xmemory (156):std::allocator<newI>::construct

   f:\vs2005\vc\include\memory (401): std::_Uninit_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

    f:\vs2005\vc\include\memory(916): stdext::unchecked_uninitialized_fill_n<newI *,unsignedint,newI,std::allocator<newI> >

   f:\vs2005\vc\include\vector (1208):std::vector<newI,std::allocator<newI> >::_Ufill

   f:\vs2005\vc\include\vector (536): std::vector<newI,std::allocator<newI>>::_Construct_n

   f:\vs2005\vc\include\vector (470):std::vector<newI,std::allocator<newI>>::vector<newI,std::allocator<newI> >

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   01 00 00 00                                                  ........ ........

 

---------- Block 180 at 0x003ACDA0: 4 bytes----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (18): newI::newI

   f:\vs2005\vc\include\xmemory (53): std::_Construct<newI,newI>

   f:\vs2005\vc\include\xmemory (156):std::allocator<newI>::construct

   f:\vs2005\vc\include\memory (401): std::_Uninit_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

   f:\vs2005\vc\include\memory (916):stdext::unchecked_uninitialized_fill_n<newI *,unsigned int,newI,std::allocator<newI>>

   f:\vs2005\vc\include\vector (1208):std::vector<newI,std::allocator<newI> >::_Ufill

   f:\vs2005\vc\include\vector (536):std::vector<newI,std::allocator<newI> >::_Construct_n

   f:\vs2005\vc\include\vector (470): std::vector<newI,std::allocator<newI>>::vector<newI,std::allocator<newI> >

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   01 00 00 00                                                 ........ ........

 

---------- Block 179 at 0x003ACD60: 4 bytes----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (18): newI::newI

   f:\vs2005\vc\include\xmemory (53): std::_Construct<newI,newI>

   f:\vs2005\vc\include\xmemory (156):std::allocator<newI>::construct

   f:\vs2005\vc\include\memory (401): std::_Uninit_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

   f:\vs2005\vc\include\memory (916):stdext::unchecked_uninitialized_fill_n<newI *,unsignedint,newI,std::allocator<newI> >

   f:\vs2005\vc\include\vector (1208): std::vector<newI,std::allocator<newI>>::_Ufill

   f:\vs2005\vc\include\vector (536):std::vector<newI,std::allocator<newI> >::_Construct_n

   f:\vs2005\vc\include\vector (470):std::vector<newI,std::allocator<newI>>::vector<newI,std::allocator<newI> >

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available): RegisterWaitForInputIdle

 Data:

   01 00 00 00                                                 ........ ........

 

---------- Block 178 at 0x003ACD20: 4 bytes----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (18): newI::newI

   f:\vs2005\vc\include\xmemory (53): std::_Construct<newI,newI>

   f:\vs2005\vc\include\xmemory (156):std::allocator<newI>::construct

   f:\vs2005\vc\include\memory (401): std::_Uninit_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

   f:\vs2005\vc\include\memory (916):stdext::unchecked_uninitialized_fill_n<newI *,unsignedint,newI,std::allocator<newI> >

   f:\vs2005\vc\include\vector (1208):std::vector<newI,std::allocator<newI> >::_Ufill

   f:\vs2005\vc\include\vector (536):std::vector<newI,std::allocator<newI> >::_Construct_n

   f:\vs2005\vc\include\vector (470):std::vector<newI,std::allocator<newI>>::vector<newI,std::allocator<newI> >

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   01 00 00 00                                                 ........ ........

 

---------- Block 177 at 0x003ACCE0: 4 bytes----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (18): newI::newI

   f:\vs2005\vc\include\xmemory (53): std::_Construct<newI,newI>

   f:\vs2005\vc\include\xmemory (156): std::allocator<newI>::construct

   f:\vs2005\vc\include\memory (401): std::_Uninit_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

   f:\vs2005\vc\include\memory (916):stdext::unchecked_uninitialized_fill_n<newI *,unsignedint,newI,std::allocator<newI> >

    f:\vs2005\vc\include\vector (1208):std::vector<newI,std::allocator<newI> >::_Ufill

   f:\vs2005\vc\include\vector (536):std::vector<newI,std::allocator<newI> >::_Construct_n

   f:\vs2005\vc\include\vector (470):std::vector<newI,std::allocator<newI> >::vector<newI,std::allocator<newI>>

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   01 00 00 00                                                 ........ ........

 

---------- Block 176 at 0x003ACCA0: 4 bytes----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (18): newI::newI

   f:\vs2005\vc\include\xmemory (53): std::_Construct<newI,newI>

   f:\vs2005\vc\include\xmemory (156):std::allocator<newI>::construct

   f:\vs2005\vc\include\memory (401): std::_Uninit_fill_n<newI*,unsigned int,newI,std::allocator<newI> >

   f:\vs2005\vc\include\memory (916):stdext::unchecked_uninitialized_fill_n<newI *,unsignedint,newI,std::allocator<newI> >

   f:\vs2005\vc\include\vector (1208):std::vector<newI,std::allocator<newI> >::_Ufill

   f:\vs2005\vc\include\vector (536):std::vector<newI,std::allocator<newI> >::_Construct_n

   f:\vs2005\vc\include\vector (470):std::vector<newI,std::allocator<newI>>::vector<newI,std::allocator<newI> >

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   01 00 00 00                                                 ........ ........

 

---------- Block 175 at 0x003ACC38: 40bytes ----------

 Call Stack:

   f:\vs2005\vc\include\xmemory (44): std::_Allocate<newI>

   f:\vs2005\vc\include\xmemory (146): std::allocator<newI>::allocate

   f:\vs2005\vc\include\vector (1074):std::vector<newI,std::allocator<newI> >::_Buy

   f:\vs2005\vc\include\vector (533):std::vector<newI,std::allocator<newI> >::_Construct_n

   f:\vs2005\vc\include\vector (470):std::vector<newI,std::allocator<newI>>::vector<newI,std::allocator<newI> >

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

    f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c(414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   A0 CC 3A 00    E0 CC 3A 00    20 CD 3A 00    60 CD 3A 00     ..:...:. ..:.`.:.

   A0 CD 3A 00    E0 CD 3A 00    20 CE 3A 00    60 CE 3A 00     ..:...:. ..:.`.:.

   A0 CE 3A 00    E0 CE 3A 00                                   ..:...:.........

 

---------- Block 173 at 0x003ACBA8: 20bytes ----------

 Call Stack:

   e:\ctemp\2005check_1\2005check\2005check.cpp (33): wmain

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (594): __tmainCRTStartup

   f:\sp\vctools\crt_bld\self_x86\crt\src\crtexe.c (414): wmainCRTStartup

   0x7C816FD7 (File and line number not available):RegisterWaitForInputIdle

 Data:

   00 00 00 00    CD CD CD CD    38 CC 3A 00    60 CC 3A 00     ........ 8.:.`.:.

   60 CC 3A 00                                                 `.:..... ........

 

Visual Leak Detector detected 12 memoryleaks.

“2005check.exe”: 已卸载“C:\WINDOWS\system32\dbghelp.dll”

“2005check.exe”: 已卸载“C:\WINDOWS\system32\version.dll”

Visual Leak Detector is now exiting.

程序“[1384] 2005check.exe:本机”已退出,返回值为 0 (0x0)。

0 0
原创粉丝点击