VS2019使用VLD(Visual Leak Detector)检测CPP内存泄漏
VS2019使用VLD(Visual Leak Detector)检测CPP内存泄漏
环境:
编译器: VS2019
VLD: 2.5.1
前言
在windows平台下,VLD(Visual Leak Detector)是一个比较准确的内存泄漏检测工具,支持输出检测报告。
下载地址:https://github.com/KindDragon/vld
1. 安装
下载路径:
https://github.com/KindDragon/vld/releases/download/v2.5.1/vld-2.5.1-setup.exe
双击安装即可
2. 编写测试代码
- main.cpp
#include "vld.h"
int main()
{
char *s = new char[1024];
return 0;
}
- CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(main)
# release enable vld
add_definitions(-DVLD_FORCE_ENABLE)
# set vld include and lib
SET(VLD_PATH "C:/Program Files (x86)/Visual Leak Detector")
SET(VLD_INCLUDE "${VLD_PATH}/include")
SET(VLD_LIB "${VLD_PATH}/lib/Win64/vld.lib")
add_executable( ${PROJECT_NAME} main.cpp)
- 编译为64位RelWithDebInfo
cmake -B bin -Thost=x64
cmake --build bin --config=RelWithDebInfo
3. 结果
main.exe
Visual Leak Detector read settings from: C:\Program Files (x86)\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5.1 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00000000600A8B60: 1024 bytes ----------
Leak Hash: 0xBF161843, Count: 1, Total 1024 bytes
Call Stack (TID 15580):
ucrtbase.dll!malloc_base()
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\heap\new_scalar.cpp (35): main.exe!operator new() + 0x8 bytes
C:\memoryLeakTest\main.cpp (5): main.exe!main()
D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (288): main.exe!__scrt_common_main_seh() + 0x22 bytes
KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
ntdll.dll!RtlUserThreadStart() + 0x21 bytes
Data:
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ........ ........
Visual Leak Detector detected 1 memory leak (1024 bytes).
Largest number used: 1024 bytes.
Total allocations: 1024 bytes.
Visual Leak Detector is now exiting.
4. 其他
4.1 报告输出到文件
复制C:\Program Files (x86)\Visual Leak Detector\vld.ini
文件到main.exe所在目录
修改vld.ini中的ReportTo = debugger
为ReportTo = both
然后运行main.exe会同时输出信息到控制台和memory_leak_report.txt文件
4.2 输出指定名称的报告文件
VLDSetReportOptions(VLD_OPT_REPORT_TO_FILE,L"main.txt");