【目标】使用VLD检测程序中的内存泄露
【参考】
《VLD(C++内存检测工具学习2) 》 http://blog.csdn.net/zhangshan415/article/details/8022050
一、安装
下载路径: http://vld.codeplex.com/
下载后安装即可用
二、配置
(1)要在工程中启用 VLD 只需要引用其头文件 "vld.h" 并使之工作即可。
为了达到这个目的,需要添加其头文件路径和LIB库
以下假设安装的路径是 $VLD_PATH = E:\Visual Leak Detector
(2)添加头文件路径,工程属性 -> C/C++ -> 常规 -> 附加包含目录 , 在该项中添加 E:\Visual Leak Detector\include
在main函数所在文件中,添加 #include "vld.h" 即可
(3)添加LIB库,工程属性 -> 链接器 -> 常规 -> 附加库目录 , 在该项中添加 E:\Visual Leak Detector\lib\Win32 (根据机器平台选择 WIN32或WIN64)。
工程属性 -> 链接器 -> 输入 -> 附加依赖项,在该项中添加 vld.lib
至此配置结束
三、使用
确保已经include "vld.h", F5调试即可。
如果没有内存泄露会提示
No memory leaks detected.
Visual Leak Detector is now exiting.
否则会给出内存泄露的地方,例如下面这段程序:
#include <iostream> #include "vld.h" void main() { std::cout << "start test" << std::endl; char * leak = new char[10]; }
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00EF4A20: 10 bytes ----------
Call Stack:
f:\workspace\vs\vld_test\vld_test\main.cpp (7): VLD_Test.exe!main + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (555): VLD_Test.exe!__tmainCRTStartup + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): VLD_Test.exe!mainCRTStartup
0x75F233AA (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
0x77839EF2 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
0x77839EC5 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
Data:
CD CD CD CD CD CD CD CD CD CD ........ ........
双击 “f:\workspace\vs\vld_test\vld_test\main.cpp (7): VLD_Test.exe!main + 0x7 bytes” 这句话,可以跳转到具体泄露的地点。