【cpp/c++ summary 工具】 vld(Visual Leak Detector)windows 内存泄漏检测工具
Visual Leak Detector,这是一个用于检测C/C++程序内存泄漏的工具。它可以在开发Windows应用程序时发现并修复内存泄漏的问题。确保在项目设置中链接到VLD的库。这样可以在程序启动时初始化VLD。
·
- Visual Leak Detector,这是一个用于检测C/C++程序内存泄漏的工具。它可以在开发Windows应用程序时发现并修复内存泄漏的问题。
安装VLD
运行程序
在项目中包含头文件
- 项目中,通常需要在
main.cpp或相应的入口点文件中包含vld.h头文件。这样可以在程序启动时初始化VLD。
// main.cpp 或者项目的入口文件
#include "vld.h"
链接VLD库
- 如果你使用的是Visual Studio,可以在项目属性中进行如下操作:
- 右键打开项目属性页面 (
Properties或Alt + Enter)。 - 转到
VC++目录->包含目录添加VLD库的位置。 - 转到
VC++目录->库目录添加VLD.lib所在目录位置。

#include "vld.h" // 引入VLD头文件
#include <iostream>
int main() {
std::cout << "Starting program..." << std::endl;
int* p = new int(5); // 分配内存
// delete p; // 释放内存
std::cout << "Program finished." << std::endl;
return 0;
}
- 构建并运行程序。VLD会在程序退出时报告内存泄漏情况。
“testvld.exe”: 已加载“C:\Users\audit\Documents\Visual Studio 2010\Projects\testvld\Debug\testvld.exe”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\ntdll.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\kernel32.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\KernelBase.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcp100d.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Program Files (x86)\Visual Leak Detector\bin\Win32\vld_x86.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcr100d.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\advapi32.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcrt.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\sechost.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\bcrypt.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\rpcrt4.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Program Files (x86)\Visual Leak Detector\bin\Win32\dbghelp.dll”,Cannot find or open the PDB file
Visual Leak Detector read settings from: C:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
线程 'Win32 线程' (0x3328) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x2144) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x29dc) 已退出,返回值为 0 (0x0)。
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x014248D8: 4 bytes ----------
Leak Hash: 0xFDB1D2BB, Count: 1, Total 4 bytes
Call Stack (TID 4060):
MSVCR100D.dll!operator new()
c:\users\audit\documents\visual studio 2010\projects\testvld\testvld\main1.cpp (7): testvld.exe!main() + 0x7 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (555): testvld.exe!__tmainCRTStartup() + 0x19 bytes
f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (371): testvld.exe!mainCRTStartup()
KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
ntdll.dll!RtlInitializeExceptionChain() + 0x6B bytes
ntdll.dll!RtlClearBits() + 0xBF bytes
Data:
05 00 00 00 ........ ........
Visual Leak Detector detected 1 memory leak (40 bytes).
Largest number used: 40 bytes.
Total allocations: 40 bytes.
Visual Leak Detector is now exiting.
程序“[8448] testvld.exe: 本机”已退出,返回值为 0 (0x0)。
解决内存泄漏
- 根据VLD提供的报告,检查代码中的内存分配和释放逻辑,以确保所有的
new或malloc都有对应的delete或free调用。
#include "vld.h" // 引入VLD头文件
#include <iostream>
int main() {
std::cout << "Starting program..." << std::endl;
int* p = new int(5); // 分配内存
// delete p; // 释放内存
std::cout << "Program finished." << std::endl;
return 0;
}
- 输出:
No memory leaks detected.
“testvld.exe”: 已加载“C:\Users\audit\Documents\Visual Studio 2010\Projects\testvld\Debug\testvld.exe”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\ntdll.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\kernel32.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\KernelBase.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcr100d.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcp100d.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Program Files (x86)\Visual Leak Detector\bin\Win32\vld_x86.dll”,已加载符号。
“testvld.exe”: 已加载“C:\Windows\SysWOW64\advapi32.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\msvcrt.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\sechost.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\bcrypt.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Windows\SysWOW64\rpcrt4.dll”,Cannot find or open the PDB file
“testvld.exe”: 已加载“C:\Program Files (x86)\Visual Leak Detector\bin\Win32\dbghelp.dll”,Cannot find or open the PDB file
Visual Leak Detector read settings from: C:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
线程 'Win32 线程' (0xcd0) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x3ca0) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x3738) 已退出,返回值为 0 (0x0)。
No memory leaks detected.
Visual Leak Detector is now exiting.
程序“[10956] testvld.exe: 本机”已退出,返回值为 0 (0x0)。
更多推荐

所有评论(0)