简介
Windows Leaks Detector is a tool for easy detection of memory leaks in any Windows application. Main features:
- No modifications in source code. Actually the code is not required.
- Works for any Windows application written in any language.
- Attaching to any running process.
- Especially effective for applications working in “cyclic” patterns.
- Aggregation of memory leaks by call stack.
下载安装
下载地址:https://kinddragon.github.io/vld/。

安装一路Next即可,其中注意一点:勾选Add VLD directory to your environmental path与Add VLD diretory to VS 2010-VS 2015(我的是VS2015)。

使用
使用VLD只需要引用相关头文件即可,#include <vld.h>。
下面是一个使用VLD进行内存泄漏检查的例子:
有内存泄漏
#include <vld.h>
int main()
{
auto* pData1 = new int;
auto* pData2 = new int;
return 0;
}
直接在VisualStudio中运行(开始调试[F5]),输出窗口中输入如下信息:
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 2 at 0x00C57620: 4 bytes ----------
Leak Hash: 0xB17261AF, Count: 1, Total 4 bytes
Call Stack (TID 1352):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): TestVLD.exe!operator new() + 0x9 bytes
c:\users\xupeng\desktop\testvld\testvld\main.cpp (6): TestVLD.exe!main() + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (64): TestVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (255): TestVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (300): TestVLD.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): TestVLD.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
Data:
CD CD CD CD ........ ........
---------- Block 1 at 0x00C576E0: 4 bytes ----------
Leak Hash: 0x8C97F628, Count: 1, Total 4 bytes
Call Stack (TID 1352):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): TestVLD.exe!operator new() + 0x9 bytes
c:\users\xupeng\desktop\testvld\testvld\main.cpp (5): TestVLD.exe!main() + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (64): TestVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (255): TestVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (300): TestVLD.exe!__scrt_common_main()
f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): TestVLD.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
Data:
CD CD CD CD ........ ........
Visual Leak Detector detected 2 memory leaks (80 bytes).
Largest number used: 80 bytes.
Total allocations: 80 bytes.
Visual Leak Detector is now exiting.
Visual Leak Detector检测到两处内存泄漏,相关的信息在Block 1和Block 2中。
另外在输出窗口,点击main.cpp所在行,可以直接在代码中定位到内存泄漏相关代码所在行:

无内存泄漏
如果没有内存泄漏,运行结果如下:
#include <vld.h>
int main()
{
auto* pData1 = new int;
auto* pData2 = new int;
delete pData1;
delete pData2;
return 0;
}
No memory leaks detected.
Visual Leak Detector is now exiting.
其他注意事项
1 如果有include"stdafx.h",则include <vld.h>放在其后,否则放在最前面
2 VLD只在Debug版本有效
3 如果想将产生的日志保存到文件中,需要将vld.ini(VLD安装目录下)复制到可执行文件目录下,然后作如下修改:
ReportFile =.\memory_leak_report.txt
ReportTo = both
本文介绍了WindowsLeaksDetector (VLD),一款无需修改源代码的内存泄漏检测工具,适用于各种语言的Windows应用。通过示例展示了如何使用VLD检测内存泄漏,并提供了安装、配置和使用技巧,包括注意事项和配置日志保存。

2856

被折叠的 条评论
为什么被折叠?



