说明
使用 VLD 内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍 VLD 配置文件中配置项 ReportFile 的使用方法。同系列文章目录可见 《内存泄漏检测工具》目录
1. 配置文件使用说明
在程序中通过 #include "vld.h"
的方式检测内存泄漏时,VLD 首先会尝试在程序的生成目录下读取 vld.ini
文件,若未读取成功,则会尝试在 VLD 的安装目录下读取 vld.ini
文件,若仍未读取成功,则会使用内置的默认配置,内置的默认配置如果不动源码是无法更改的,因此通过修改相应目录下的 vld.ini
文件来定制 VLD 功能是最好的选择。当配置参数等号右边为空,或者给配置了不合法值时,在使用过程中会被程序重置到默认值。
2. 设置输出文件的路径
参数名:ReportFile
。
有效赋值:合法且包含文件名的文件路径。
默认值:.\memory_leak_report.txt
。
功能说明:当 ReportFile = file
或 ReportFile = both
时,这个配置项才能起到作用,设置报告输出的文件路径及文件名,这个路径可以是绝对路径,也可以是相对路径,默认路径是生成目录,默认文件名是 memory_leak_report.txt
。
2.1 测试代码
#include <QCoreApplication>
#include "vld.h"
void testFun(int i)
{
int *ptr = new int(i);
printf("ptr = %08x, *ptr = %08x.\n", ptr, *ptr);
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testFun(1);
return a.exec();
}
测试环境:QT 5.9.2,MSVC 2015 32bit,Debug 模式,VLD 版本为 2.5.1,VLD 配置文件除了对该参数做修改外,还将 ReportFile
设置为 file
,测试工程所在路径为:E:\Cworkspace\Qt 5.9\QtDemo\testVLD
。
2.2 ReportFile 为空时的输出
标准输出窗显示:
ptr = 01027298, *ptr = 00000001.
VLD 输出报告(控制台):
Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
VLD 输出报告(文件 memory_leak_report.txt
):
Visual Leak Detector Version 2.5.1 installed.
Outputting the report to E:\Cworkspace\Qt 5.9\QtDemo\build-testVLD-Desktop_Qt_5_9_2_MSVC2015_32bit-Debug\memory_leak_report.txt
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x01027298: 4 bytes ----------
Leak Hash: 0x78A7E46A, Count: 1, Total 4 bytes
Call Stack (TID 27760):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (14): testVLD.exe!main() + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): 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:
01 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.
2.3 ReportFile 指定中文文件名时的输出
设置如下:
ReportFile = E:\Cworkspace\Qt 5.9\QtDemo\中文文件.txt
标准输出窗显示:
ptr = 00dea598, *ptr = 00000001.
VLD 输出报告(控制台):
Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
在路径 E:\Cworkspace\Qt 5.9\QtDemo
下生成了以下文件,中文文件名乱码了。
VLD 输出报告(文件中),文件内容没有乱码:
Visual Leak Detector Version 2.5.1 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00DEA598: 4 bytes ----------
Leak Hash: 0x78A7E46A, Count: 1, Total 4 bytes
Call Stack (TID 21832):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (14): testVLD.exe!main() + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): 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:
01 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.
2.3 ReportFile 指定中文路径时的输出
设置如下:
ReportFile = E:\Cworkspace\Qt 5.9\QtDemo\中文路径\test.txt
标准输出窗显示:
ptr = 00cfb310, *ptr = 00000001.
VLD 输出报告(控制台):
Visual Leak Detector read settings from: D:\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 0x00CFB310: 4 bytes ----------
Leak Hash: 0x78A7E46A, Count: 1, Total 4 bytes
Call Stack (TID 10592):
ucrtbased.dll!malloc()
f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun() + 0x7 bytes
e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (14): testVLD.exe!main() + 0x7 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): 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:
01 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.
在指定路径下并没有找到输出报告,因此中文路径在 VLD 里面是不合法的,输出被重置到了控制台中。
2.5 输出结果对比
- 当
ReportFile
为空时,将在生成目录下输出到文件memory_leak_report.txt
中。 - 当
ReportFile
指定中文文件名时,报告能正常输出到指定文件,但文件名会乱码。 - 当
ReportFile
指定中文路径时,报告会被重置输出到控制台,不能输出到指定文件。
- ReportFile Detector Visual Leakreportfile detector visual leak detector visual leak vld detector方式visual leak skipheapfreeleaks detector visual leak 源码detector visual leak detector visual leak 2015 skipcrtstartupleaks detector visual leak forceincludemodulesmd detector visual leak stackwalkmethod detector visual leak detector版本visual leak