设为首页 加入收藏

TOP

【Visual Leak Detector】QT 中 VLD 输出解析(三)(二)
2023-07-23 13:35:00 】 浏览:77
Tags:Visual Leak Detector VLD 解析
!BaseThreadInitThunk() + 0x19 bytes ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0x11E bytes ntdll.dll!RtlGetAppContainerNamedObjectPath() + 0xEE bytes Data: 78 56 34 55 xV4U.... ........ ---------- Block 3 at 0x00B674E8: 3 bytes ---------- Leak Hash: 0x1ED7DC7D, Count: 1, Total 3 bytes Call Stack (TID 14904): ucrtbased.dll!malloc() f:\dd\vctools\crt\vcstartup\src\heap\new_array.cpp (15): testVLD.exe!operator new[]() + 0x9 bytes e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!testFun3() + 0x7 bytes e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (30): testVLD.exe!main() 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: CD CD CD ........ ........ Visual Leak Detector detected 3 memory leaks (117 bytes). Largest number used: 117 bytes. Total allocations: 117 bytes. Visual Leak Detector is now exiting.

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

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

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

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

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

#include <QCoreApplication>
#include "vld.h"

void testFun1()
{
    int *ptr = new int(0x55345678);
    printf("ptr1 = %08x, *ptr1 = %08x.\n", ptr, *ptr);
}

void testFun2()
{
    short *ptr = new short(0x4529);
    printf("ptr2 = %08x, *ptr2 = %04x.\n", ptr, *ptr);
    delete ptr;
}

void testFun3()
{
    char *ptr = new char[3];
    printf("ptr3 = %08x.\n", ptr, *ptr);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    testFun1();
    testFun2();
    testFun3();

    return a.exec();
}

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

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

程序运行结束后,检测到了内存泄漏,VLD 会输出以下报告(本例中出现两处内存泄漏),第 1~3 行显示 VLD 运行状态,第 4~19 行显示 testFun1() 函数泄漏内存的详细信息,第 22~37 行显示 testFun3() 函数泄漏内存的详细信息,第 40~42 行总结此次泄漏情况,第 43 行显示 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 0x006C5C68: 4 bytes ----------
  Leak Hash: 0x1E4EE072, Count: 1, Total 4 bytes
  Call Stack (TID 11992):
    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!testFun1() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\te
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇What is static and dynamic libr.. 下一篇Qt源码阅读(二) moveToThread

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目