天天看点

内存泄漏检查工具 Visual Leak Detector(VLD)简介下载安装使用其他注意事项

简介

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/。

内存泄漏检查工具 Visual Leak Detector(VLD)简介下载安装使用其他注意事项

安装一路Next即可,其中注意一点:勾选

Add VLD directory to your environmental path

Add VLD diretory to VS 2010-VS 2015

(我的是VS2015)。

内存泄漏检查工具 Visual Leak Detector(VLD)简介下载安装使用其他注意事项

使用

使用

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

所在行,可以直接在代码中定位到内存泄漏相关代码所在行:

内存泄漏检查工具 Visual Leak Detector(VLD)简介下载安装使用其他注意事项

无内存泄漏

如果没有内存泄漏,运行结果如下:

#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

继续阅读