首页 > 其他分享 >Qt Visual Leak Detector 输出解析(三)

Qt Visual Leak Detector 输出解析(三)

时间:2024-03-04 18:45:51浏览次数:30  
标签:exe Qt Leak bytes Visual main testVLD Block vcstartup

1. 使用方式

  在 QT 中使用 VLD 的方法可以查看另外几篇博客:

  • QT 使用 Visual Leak Detector(方式一)
  • QT 使用 Visual Leak Detector(方式二)
  • QT 使用 Visual Leak Detector(方式三)

  本次测试使用的环境为:QT 5.9.2,MSVC 2015 32bit,Debug 模式,VLD 版本为 2.5.1,VLD 配置文件不做任何更改使用默认配置,测试工程所在路径为:E:\Cworkspace\Qt 5.9\QtDemo\testVLD。

2. 有三处内存泄漏时的输出报告

  写一个有三处内存泄漏的程序,如下:

 1 #include <QCoreApplication>
 2 #include "vld.h"
 3 
 4 void testFun1()
 5 {
 6     int *ptr = new int(0x55345678);
 7     printf("ptr1 = %08x, *ptr1 = %08x.\n", ptr, *ptr);
 8 }
 9 
10 void testFun2()
11 {
12     short *ptr = new short(0x4529);
13     printf("ptr2 = %08x, *ptr2 = %04x.\n", ptr, *ptr);
14 }
15 
16 void testFun3()
17 {
18     char *ptr = new char[3];
19     printf("ptr3 = %08x.\n", ptr, *ptr);
20 }
21 
22 int main(int argc, char *argv[])
23 {
24     QCoreApplication a(argc, argv);
25 
26     testFun1();
27     testFun2();
28     testFun3();
29 
30     return a.exec();
31 }

程序运行时,在标准输出窗会输出以下结果:

1 ptr1 = 00b674b8, *ptr1 = 55345678.
2 ptr2 = 00b670f8, *ptr2 = 4529.
3 ptr3 = 00b674e8.

  程序运行结束后,检测到了内存泄漏,VLD 会输出以下报告(本例中出现三处内存泄漏),第 1~3 行显示 VLD 运行状态,第 4~19 行显示 testFun2() 函数泄漏内存的详细信息,第 22~37 行显示 testFun1() 函数泄漏内存的详细信息,第 40~55 行显示 testFun3() 函数泄漏内存的详细信息,第 58~60 行总结此次泄漏情况,第 61 行显示 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 2 at 0x00B670F8: 2 bytes ----------
 5   Leak Hash: 0xB9FC7D06, Count: 1, Total 2 bytes
 6   Call Stack (TID 14904):
 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 (12): testVLD.exe!testFun2() + 0x7 bytes
10     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (28): 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     29 45                                                        )E...... ........
20 
21 
22 ---------- Block 1 at 0x00B674B8: 4 bytes ----------
23   Leak Hash: 0xE622CBED, Count: 1, Total 4 bytes
24   Call Stack (TID 14904):
25     ucrtbased.dll!malloc()
26     f:\dd\vctools\crt\vcstartup\src\heap\new_scalar.cpp (19): testVLD.exe!operator new() + 0x9 bytes
27     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (6): testVLD.exe!testFun1() + 0x7 bytes
28     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (27): testVLD.exe!main()
29     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
30     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
31     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
32     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
33     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
34     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
35     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
36   Data:
37     78 56 34 55                                                  xV4U.... ........
38 
39 
40 ---------- Block 3 at 0x00B674E8: 3 bytes ----------
41   Leak Hash: 0x1ED7DC7D, Count: 1, Total 3 bytes
42   Call Stack (TID 14904):
43     ucrtbased.dll!malloc()
44     f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): testVLD.exe!operator new[]() + 0x9 bytes
45     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!testFun3() + 0x7 bytes
46     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (30): testVLD.exe!main()
47     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
48     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
49     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
50     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
51     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
52     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
53     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
54   Data:
55     CD CD CD                                                     ........ ........
56 
57 
58 Visual Leak Detector detected 3 memory leaks (117 bytes).
59 Largest number used: 117 bytes.
60 Total allocations: 117 bytes.
61 Visual Leak Detector is now exiting.

  观察以上输出报告,可以总结出以下规律:

  Block 后面的序号和函数调用顺序有关,先调用的序号小,在主函数中 testFun1()、testFun2()、testFun3() 是依次调用的,分别对应于 Block 1、Block 2、Block 3。
报告输出时泄漏信息的显示顺序和地址大小有关,地址小的先显示,Block 1、Block 2、Block 3 的泄漏首地址分别为 0x00B674B8、0x00B670F8、0x00B674E8,内存地址从小到大排序为 Block 2、Block 1、Block 3,显示顺序正是如此。

  每个 Block 输出的详细解析可以查看另外一篇博客 【Visual Leak Detector】QT 中 VLD 输出解析(二)。第 58~60 行中的 117 bytes 包含有: Block 1 中申请 int 的 4 bytes 及对应的 36 bytes 内存管理头、 Block 2 中申请 short 的 2 bytes 及对应的 36 bytes 内存管理头、 Block 3 中申请 char[3] 的 3 bytes 及对应的 36 bytes 内存管理头,共计 4 + 36 + 2 + 36 + 3 + 36 = 117 b y t e s 4 + 36 + 2 + 36 + 3 + 36 = 117 bytes4+36+2+36+3+36=117bytes。

3. 有两处内存泄漏时的输出报告

  将 testFun2() 函数中申请的内存正常释放,测试代码如下:

 1 #include <QCoreApplication>
 2 #include "vld.h"
 3 
 4 void testFun1()
 5 {
 6     int *ptr = new int(0x55345678);
 7     printf("ptr1 = %08x, *ptr1 = %08x.\n", ptr, *ptr);
 8 }
 9 
10 void testFun2()
11 {
12     short *ptr = new short(0x4529);
13     printf("ptr2 = %08x, *ptr2 = %04x.\n", ptr, *ptr);
14     delete ptr;
15 }
16 
17 void testFun3()
18 {
19     char *ptr = new char[3];
20     printf("ptr3 = %08x.\n", ptr, *ptr);
21 }
22 
23 int main(int argc, char *argv[])
24 {
25     QCoreApplication a(argc, argv);
26 
27     testFun1();
28     testFun2();
29     testFun3();
30 
31     return a.exec();
32 }

  程序运行时,在标准输出窗会输出以下结果:

1 ptr1 = 006c5c68, *ptr1 = 55345678.
2 ptr2 = 006c5cc8, *ptr2 = 4529.
3 ptr3 = 006c5db8.

  程序运行结束后,检测到了内存泄漏,VLD 会输出以下报告(本例中出现两处内存泄漏),第 1~3 行显示 VLD 运行状态,第 4~19 行显示 testFun1() 函数泄漏内存的详细信息,第 22~37 行显示 testFun3() 函数泄漏内存的详细信息,第 40~42 行总结此次泄漏情况,第 43 行显示 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 0x006C5C68: 4 bytes ----------
 5   Leak Hash: 0x1E4EE072, Count: 1, Total 4 bytes
 6   Call Stack (TID 11992):
 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!testFun1() + 0x7 bytes
10     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (28): 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 
21 
22 ---------- Block 3 at 0x006C5DB8: 3 bytes ----------
23   Leak Hash: 0xE91F4A96, Count: 1, Total 3 bytes
24   Call Stack (TID 11992):
25     ucrtbased.dll!malloc()
26     f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): testVLD.exe!operator new[]() + 0x9 bytes
27     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (19): testVLD.exe!testFun3() + 0x7 bytes
28     e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (31): testVLD.exe!main()
29     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (74): testVLD.exe!invoke_main() + 0x1B bytes
30     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (264): testVLD.exe!__scrt_common_main_seh() + 0x5 bytes
31     f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl (309): testVLD.exe!__scrt_common_main()
32     f:\dd\vctools\crt\vcstartup\src\startup\exe_main.cpp (17): testVLD.exe!mainCRTStartup()
33     KERNEL32.DLL!BaseThreadInitThunk() + 0x19 bytes
34     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes
35     ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes
36   Data:
37     CD CD CD                                                     ........ ........
38 
39 
40 Visual Leak Detector detected 2 memory leaks (79 bytes).
41 Largest number used: 79 bytes.
42 Total allocations: 117 bytes.
43 Visual Leak Detector is now exiting.

  观察以上输出报告,可知 Block 后面的序号不仅仅针对导致内存泄漏的函数,这个例子中的 Block 2 应该属于 testFun2(),但由于 testFun2() 正常释放了内存,因此在输出报告中没有找到 Block 2,只能找到 Block 1 和 Block 3,这个例子说明了输出报告中 Block 序号不一定是连续的,可能缺失了一些序号,属正常现象。第 40~41 行中的 79 bytes 包含有: Block 1 中申请 int 的 4 bytes 及对应的 36 bytes 内存管理头、 Block 3 中申请 char[3] 的 3 bytes 及对应的 36 bytes 内存管理头,共计 4 + 36 + 3 + 36 = 79 b y t e s 4 + 36 + 3 + 36 = 79 bytes4+36+3+36=79bytes。第 42 行中的 117 bytes 表示运行过程中一共分配的内存大小,计算方式与上一节一样。

标签:exe,Qt,Leak,bytes,Visual,main,testVLD,Block,vcstartup
From: https://www.cnblogs.com/ybqjymy/p/18052402

相关文章

  • Qt Visual Leak Detector 输出解析(四)
    1.使用方式在QT中使用VLD的方法可以查看另外几篇博客:QT使用VisualLeakDetector(方式一)QT使用VisualLeakDetector(方式二)QT使用VisualLeakDetector(方式三)本次测试使用的环境为:QT5.9.2,Debug模式,VLD版本为2.5.1,VLD配置文件不做任何更改使用默认配......
  • Qt Visual Leak Detector 输出解析(一)
    1.使用方式在QT中使用VLD的方法可以查看另外几篇博客: QT使用VisualLeakDetector(方式一)QT使用 VisualLeakDetector(方式二)QT使用 VisualLeakDetector(方式三) 本次测试使用的环境为:QT5.9.2,MSVC201532bit,Debug模式,VLD版本为2.5.1,VLD配置文......
  • QT Visual Leak Detector 输出解析(二)
    1.使用方式在QT中使用VLD的方法可以查看另外几篇博客:QT使用VisualLeakDetector(方式一)QT使用VisualLeakDetector(方式二)QT使用VisualLeakDetector(方式三)本次测试使用的环境为:QT5.9.2,MSVC201532bit,Debug模式,VLD版本为2.5.1,VLD配置文件不做任何......
  • Qt 使用 Visual Leak Detector(方式二)
    1.使用前的准备参考本人另一篇博客安装VisualLeakDetector下载vld-2.5.1-setup.exe并按步骤安装VLD。这一种使用方式的特点是,在一台电脑上安装完成后,需在项目pro文件中指明库及头文件的路径,然后在mian.cpp文件中#include"vld.h"。当把项目拷贝到别的电脑上编译......
  • Qt 使用Visual Leak Detector(方式三)
    1.使用前的准备参考本人另一篇博客安装VisualLeakDetector下载vld-2.5.1-setup.exe并按步骤安装VLD。这一种使用方式的特点是,在一台电脑上安装完成后,将VLD安装目录下的lib库及include文件拷贝到项目目录中,在项目pro文件中指明库及头文件的路径,并将vld.ini......
  • Qt 使用 Visual Leak Detector(方式一)
    1.使用前的准备参考本人另一篇博客安装VisualLeakDetector下载vld-2.5.1-setup.exe并按步骤安装VLD。这一种使用方式的缺点是,当把项目拷贝到别的电脑上编译运行时,需要按以下流程重新配置VLD环境。但优点是在一台电脑上配置完成后,用的时候十分方便,包含头文件就可以......
  • Qt MSVC使用内存泄露检测工具 VLD(Visual Leak Detector)
    一、简介VLD=VisualLeakDetector,是一款用于VisualC++的免费的内存泄露检测工具,官网 kinddragon.github.io, GitHub 。先说优点:为每个泄漏的块提供完整的堆栈跟踪,包括源文件和行号信息(如果可用)。检测大多数(如果不是全部)类型的进程内内存泄漏,包括基于COM的泄漏......
  • Visual Leak Detector 安装 VLD
    一.工具下载VisualLeakDetector官网下载地址:https://kinddragon.github.io/vld/,点击DownloadInstaller绿色按钮可下载工具的安装程序vld-2.5.1-setup.exe。也可去Git仓库releases界面下载安装程序vld-2.5.1-setup.exe。二.工具安装2.1Next2.2Iacceptt......
  • Visual Studio 性能探查器排查内存泄漏
    1、自用方法介绍【1】VS使用debug模式执行程序(注意:Release模式无法看到堆栈细节!)【2】截取内存快照2、别人方法介绍【1】首先用vsattach到进程,再点调试–窗口–显示诊断工具。注意:如果用调试–性能探测器去附加进程,则内存使用率这一项是灰的,无法勾选,但是......
  • Visual Studio 打开Qt项目
    一、下载QTVSTOOL插件1.1下载地址官方下载地址:Indexof/development_releases/vsaddin找到对应的VS版本下载1.2 安装先关闭VS,然后安装,在打开VS,就看到顶部有QTVSTOOLS 了1.3 添加路径QtOptions->Add->apth,找到你Qt安装的目录对应的如下文件夹Q......