内存泄漏检查工具 Visual Leak Detector(VLD)

本文介绍了WindowsLeaksDetector (VLD),一款无需修改源代码的内存泄漏检测工具,适用于各种语言的Windows应用。通过示例展示了如何使用VLD检测内存泄漏,并提供了安装、配置和使用技巧,包括注意事项和配置日志保存。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

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 pathAdd VLD diretory to VS 2010-VS 2015(我的是VS2015)。
安装一路Next即可。

使用

使用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 1Block 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.iniVLD安装目录下)复制到可执行文件目录下,然后作如下修改:
ReportFile =.\memory_leak_report.txt
ReportTo = both

Visual Leak Detector是一款用于Visual C++的免费的内存泄露检测工具。相比较其它的内存泄露检测工具,它在检测内存泄漏的同时,还具有如下特点: 1、 可以得到内存泄漏点的调用堆栈,如果可以的话,还可以得到其所在文件及行号; 2、 可以得到泄露内存的完整数据; 3、 可以设置内存泄露报告的级别; 4、 它是一个已经打包的lib,使用时无须编译它的源代码。而对于使用者自己的代码,也只需要做很小的改动; 5、 他的源代码使用GNU许可发布,并有详尽的文档及注释。对于想深入了解堆内存管理的读者,是一个不错的选择。 可见,从使用角度来讲,Visual Leak Detector简单易用,对于使用者自己的代码,唯一的修改是#include Visual Leak Detector的头文件后正常运行自己的程序,就可以发现内存问题。从研究的角度来讲,如果深入Visual Leak Detector源代码,可以学习到堆内存分配与释放的原理、内存泄漏检测的原理及内存操作的常用技巧等。 下面让我们来介绍如何使用这个小巧的工具。 首先解压得到vld.h, vldapi.h, vld.lib, vldmt.lib, vldmtdl l.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
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值