【Visual Leak Detector】QT 中 VLD 输出解析(四)

打印 上一主题 下一主题

主题 1019|帖子 1019|积分 3057

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
说明

使用 VLD 内存泄漏检测工具辅助开发时整理的学习笔记。

目录


1. 使用方式

在 QT 中使用 VLD 的方法可以查看另外几篇博客:
本次测试使用的环境为:QT 5.9.2Debug 模式,VLD 版本为 2.5.1,VLD 配置文件不做任何更改使用默认配置,测试工程所在路径为:E:\Cworkspace\Qt 5.9\QtDemo\testVLD。
2. 测试代码

写一个有一处内存泄漏的程序,如下:
  1. #include <QCoreApplication>
  2. #include "vld.h"
  3. void testFun()
  4. {
  5.     int *ptr = new int(0x55345678);
  6.     printf("ptr = %08x, *ptr = %08x", ptr, *ptr);
  7. }
  8. int main(int argc, char *argv[])
  9. {
  10.     QCoreApplication a(argc, argv);
  11.     testFun();
  12.     return a.exec();
  13. }
复制代码
3. 使用 32 bit 编译器时的输出

使用 MSVC 2015 32bit 编译器,程序运行时,在标准输出窗会输出以下结果:
  1. ptr = 0070a3d0, *ptr = 55345678
复制代码
程序运行结束后,检测到了内存泄漏,VLD 会输出以下报告(本例中出现一处内存泄漏),第 1~3 行显示 VLD 运行状态,第 4~21 行显示泄漏内存的详细信息,第 22~24 行总结此次泄漏情况,第 25 行显示 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 0x0070A3D0: 4 bytes ----------
  5.   Leak Hash: 0xA7ED883D, Count: 1, Total 4 bytes
  6.   Call Stack (TID 20672):
  7.     ucrtbased.dll!malloc()
  8.     f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
  9.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun() + 0x7 bytes
  10.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (16): 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.     78 56 34 55                                                  xV4U.... ........
  20. Visual Leak Detector detected 1 memory leak (40 bytes).
  21. Largest number used: 40 bytes.
  22. Total allocations: 40 bytes.
  23. Visual Leak Detector is now exiting.
复制代码
4. 使用 64 bit 编译器时的输出

使用 MSVC 2015 64bit 编译器,程序运行时,在标准输出窗会输出以下结果:
  1. ptr = 25a42da0, *ptr = 55345678
复制代码
程序运行结束后,检测到了内存泄漏,VLD 会输出以下报告(本例中出现一处内存泄漏),第 1~3 行显示 VLD 运行状态,第 4~21 行显示泄漏内存的详细信息,第 22~24 行总结此次泄漏情况,第 25 行显示 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 0x0000000025A42DA0: 4 bytes ----------
  5.   Leak Hash: 0x92ED96C9, Count: 1, Total 4 bytes
  6.   Call Stack (TID 16444):
  7.     ucrtbased.dll!malloc()
  8.     f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0xA bytes
  9.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun() + 0xA bytes
  10.     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (16): testVLD.exe!main()
  11.     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (75): testVLD.exe!invoke_main()
  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() + 0x14 bytes
  16.     ntdll.dll!RtlUserThreadStart() + 0x21 bytes
  17.   Data:
  18.     78 56 34 55                                                  xV4U.... ........
  19. Visual Leak Detector detected 1 memory leak (56 bytes).
  20. Largest number used: 56 bytes.
  21. Total allocations: 56 bytes.
  22. Visual Leak Detector is now exiting.
复制代码
5. 输出报告对比结果

使用不同位数的编译器时,输出报告的差异主要体现在以下几点:

  • 地址的表示位数不同,32 bit 编译器使用 8 位十六进制数表示,64 bit 编译器使用 16 位十六进制数表示。体现在输出的第 4 行,分别为 0x0070A3D0 和 0x0000000025A42DA0。
  • 程序启动时所调用的 Windows 操作系统函数不完全相同,32 bit 编译器调用了两次 RtlGetAppContainerNamedObjectPath() 函数和一次 BaseThreadInitThunk() 函数,64 bit 编译器只调用了一次 RtlUserThreadStart() 函数和一次 BaseThreadInitThunk() 函数,且它们调用 BaseThreadInitThunk() 函数时泄漏指令的内存偏移量不同,32 bit 编译器是 0x19 bytes,而 64 bit 编译器是 0x14 bytes。
  • 内存管理头的宽度不同,32 bit 编译器时是 36 bytes,64 bit 编译器时是 52 bytes。体现在输出的倒数第二行,分别为 Total allocations: 40 bytes 和 Total allocations: 56 bytes,将代码请求的内存大小 4 bytes 加上各自管理头的内存大小,可以得到与输出一致的结果: \(4 + 36 = 40bytes\) 及 \(4 + 52 = 56bytes\)。

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

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

滴水恩情

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表