【Visual Leak Detector】配置项 ReportEncoding

打印 上一主题 下一主题

主题 533|帖子 533|积分 1599

说明

使用 VLD 内存泄漏检测工具辅助开发时整理的学习笔记。本篇介绍 VLD 配置文件中配置项 ReportEncoding 的使用方法。 同系列文章目录可见 《内存泄漏检测工具》目录

目录


1. 配置文件使用说明

在程序中通过 #include "vld.h" 的方式检测内存泄漏时,VLD 首先会尝试在程序的生成目录下读取 vld.ini 文件,若未读取成功,则会尝试在 VLD 的安装目录下读取 vld.ini 文件,若仍未读取成功,则会使用内置的默认配置,内置的默认配置如果不动源码是无法更改的,因此通过修改相应目录下的 vld.ini 文件来定制 VLD 功能是最好的选择。当配置参数等号右边为空,或者给配置了不合法值时,在使用过程中会被程序重置到默认值。
2. 设置输出报告的编码格式

参数名:ReportEncoding。
有效赋值:ascii,unicode。
默认值:ascii。
功能说明:设置输出报告的编码格式。默认是 ascii 编码,若改为 unicode 编码,控制台输出报告的同时会自动将报告输出到文本文件中(即使没有人为设置输出到文件)。由于调试器 Debugger 是不支持 unicode 字符的,所以这个配置只对显示的内存数据形式有影响,当泄露块中包含 unicode 字符时,这个配置的效果才能体现。若项目路径中含有 unicode 字符,ascii 编码时将无法显示调用堆栈信息,改为 unicode 有时候则能正常显示,详见 StackOverflow vld displays empty call stack
2.1 测试代码
  1. #include <QCoreApplication>
  2. #include "vld.h"
  3. void testFun()
  4. {
  5.     QString str = QStringLiteral("汉字unicode");
  6.     char *ptr = new char[30]();
  7.     QByteArray Block = str.toLocal8Bit();
  8.     memcpy(ptr, Block.constData(), Block.size());
  9.     printf("ptr = %08x, *ptr = %s.\n", ptr, ptr);
  10. }
  11. int main(int argc, char *argv[])
  12. {
  13.     QCoreApplication a(argc, argv);
  14.     testFun();
  15.     return a.exec();
  16. }
复制代码
测试环境:QT 5.9.2MSVC 2015 32bitDebug 模式,VLD 版本为 2.5.1,VLD 配置文件只对该参数做修改,测试工程所在路径为:E:\Cworkspace\Qt 5.9\QtDemo\testVLD。
2.2 ReportEncoding = ascii 时的输出

标准输出窗显示:
  1. ptr = 0119aa60, *ptr = 汉字unicode.
复制代码
VLD 输出报告:
  1. Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
  2. Visual Leak Detector Version 2.5.1 installed.
  3. WARNING: Visual Leak Detector detected memory leaks!
  4. ---------- Block 1 at 0x0119AA60: 30 bytes ----------
  5.   Leak Hash: 0x7A25A79D, Count: 1, Total 30 bytes
  6.   Call Stack (TID 2596):
  7.     ucrtbased.dll!malloc()
  8.     f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): testVLD.exe!operator new[]() + 0x9 bytes
  9.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (7): testVLD.exe!testFun() + 0x10 bytes
  10.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (19): testVLD.exe!main()
  11.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
  12.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
  13.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
  14.     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
  15.     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
  16.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
  17.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
  18.   Data:
  19.     BA BA D7 D6    75 6E 69 63    6F 64 65 00    00 00 00 00     ....unic ode.....
  20.     00 00 00 00    00 00 00 00    00 00 00 00    00 00           ........ ........
  21. Visual Leak Detector detected 1 memory leak (66 bytes).
  22. Largest number used: 66 bytes.
  23. Total allocations: 66 bytes.
  24. Visual Leak Detector is now exiting.
复制代码
2.3 ReportEncoding = unicode 时的输出

标准输出窗显示:
  1. ptr = 0141bbe8, *ptr = 汉字unicode.
复制代码
VLD 输出报告(控制台):
  1. Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini
  2. Visual Leak Detector Version 2.5.1 installed.
  3. NOTE: Visual Leak Detector: Unicode-encoded reporting has been enabled, but the
  4.   debugger is the only selected report destination. The debugger cannot display
  5.   Unicode characters, so the report will also be sent to a file. If no file has
  6.   been specified, the default file name is ".\memory_leak_report.txt".
  7.     Generating a Unicode (UTF-16) encoded report.
  8.     Outputting the report to the debugger and to E:\Cworkspace\Qt 5.9\QtDemo\build-testVLD-Desktop_Qt_5_9_2_MSVC2015_32bit-Debug\memory_leak_report.txt
  9. WARNING: Visual Leak Detector detected memory leaks!
  10. ---------- Block 1 at 0x0141BBE8: 30 bytes ----------
  11.   Leak Hash: 0x7A25A79D, Count: 1, Total 30 bytes
  12.   Call Stack (TID 20200):
  13.     ucrtbased.dll!malloc()
  14.     f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): testVLD.exe!operator new[]() + 0x9 bytes
  15.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (7): testVLD.exe!testFun() + 0x10 bytes
  16.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (19): testVLD.exe!main()
  17.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
  18.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
  19.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
  20.     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
  21.     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
  22.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
  23.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
  24.   Data:
  25.     BA BA D7 D6    75 6E 69 63    6F 64 65 00    00 00 00 00     ??湵捩摯e..
  26.     00 00 00 00    00 00 00 00    00 00 00 00    00 00           ........
  27. Visual Leak Detector detected 1 memory leak (66 bytes).
  28. Largest number used: 66 bytes.
  29. Total allocations: 66 bytes.
  30. Visual Leak Detector is now exiting.
复制代码
VLD 输出报告(文件 memory_leak_report.txt):
  1. Visual Leak Detector Version 2.5.1 installed.
  2. NOTE: Visual Leak Detector: Unicode-encoded reporting has been enabled, but the
  3.   debugger is the only selected report destination. The debugger cannot display
  4.   Unicode characters, so the report will also be sent to a file. If no file has
  5.   been specified, the default file name is ".\memory_leak_report.txt".
  6.     Generating a Unicode (UTF-16) encoded report.
  7.     Outputting the report to the debugger and to E:\Cworkspace\Qt 5.9\QtDemo\build-testVLD-Desktop_Qt_5_9_2_MSVC2015_32bit-Debug\memory_leak_report.txt
  8. WARNING: Visual Leak Detector detected memory leaks!
  9. ---------- Block 1 at 0x0141BBE8: 30 bytes ----------
  10.   Leak Hash: 0x7A25A79D, Count: 1, Total 30 bytes
  11.   Call Stack (TID 20200):
  12.     ucrtbased.dll!malloc()
  13.     f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): testVLD.exe!operator new[]() + 0x9 bytes
  14.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (7): testVLD.exe!testFun() + 0x10 bytes
  15.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (19): testVLD.exe!main()
  16.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
  17.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
  18.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
  19.     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
  20.     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
  21.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
  22.     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
  23.   Data:
  24.     BA BA D7 D6    75 6E 69 63    6F 64 65 00    00 00 00 00     몺훗湵捩摯e..
  25.     00 00 00 00    00 00 00 00    00 00 00 00    00 00           ........
  26. Visual Leak Detector detected 1 memory leak (66 bytes).
  27. Largest number used: 66 bytes.
  28. Total allocations: 66 bytes.
  29. Visual Leak Detector is now exiting.
复制代码
2.4 输出结果对比


  • 当 ReportEncoding = ascii 时,泄漏内存中的数据(汉字unicode)只有 unicode 显示正确,根据句点 . 的个数可知一行显示 16 个 ascii 字符。
  • 当 ReportEncoding = unicode 时,报告会同时输出到生成目录下,默认文件名为 memory_leak_report.txt,根据句点 . 的个数可知一行显示 8 个 unicode 字符。奇怪的是,不论是在控制台中还是在文件中,泄漏内存中的汉字并没有按期望正常显示,只有最后一个字母 e 显示正确,这一现象产生的原因还未找到。对比控制台输出与文件输出,文件输出少了第一行 Visual Leak Detector read settings from: D:\Program Files (x86)\Visual Leak Detector\vld.ini,泄漏数据的显示也略有差别,其他部分两者完全保持一致。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

光之使者

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表