【Visual Leak Detector】QT 中 VLD 輸出解析(三)

来源:https://www.cnblogs.com/young520/archive/2023/03/26/17259898.html
-Advertisement-
Play Games

使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記。本篇介紹在 QT 中使用 VLD 時,有多處記憶體泄漏時的輸出報告解析。 ...


說明

使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記。

目錄


1. 使用方式

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

本次測試使用的環境為:QT 5.9.2MSVC 2015 32bitDebug 模式,VLD 版本為 2.5.1,VLD 配置文件不做任何更改使用預設配置,測試工程所在路徑為:E:\Cworkspace\Qt 5.9\QtDemo\testVLD

2. 有三處記憶體泄漏時的輸出報告

寫一個有三處記憶體泄漏的程式,如下:

#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);
}

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 = 00b674b8, *ptr1 = 55345678.
ptr2 = 00b670f8, *ptr2 = 4529.
ptr3 = 00b674e8.

程式運行結束後,檢測到了記憶體泄漏,VLD 會輸出以下報告(本例中出現三處記憶體泄漏),第 1~3 行顯示 VLD 運行狀態,第 4~19 行顯示 testFun2() 函數泄漏記憶體的詳細信息,第 22~37 行顯示 testFun1() 函數泄漏記憶體的詳細信息,第 40~55 行顯示 testFun3() 函數泄漏記憶體的詳細信息,第 58~60 行總結此次泄漏情況,第 61 行顯示 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 2 at 0x00B670F8: 2 bytes ----------
  Leak Hash: 0xB9FC7D06, Count: 1, Total 2 bytes
  Call Stack (TID 14904):
    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 (12): testVLD.exe!testFun2() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (28): 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:
    29 45                                                        )E...... ........


---------- Block 1 at 0x00B674B8: 4 bytes ----------
  Leak Hash: 0xE622CBED, Count: 1, Total 4 bytes
  Call Stack (TID 14904):
    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\testvld\main.cpp (27): 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:
    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\testvld\main.cpp (28): 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:
    78 56 34 55                                                  xV4U.... ........


---------- Block 3 at 0x006C5DB8: 3 bytes ----------
  Leak Hash: 0xE91F4A96, Count: 1, Total 3 bytes
  Call Stack (TID 11992):
    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 (19): testVLD.exe!testFun3() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (31): 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 2 memory leaks (79 bytes).
Largest number used: 79 bytes.
Total allocations: 117 bytes.
Visual Leak Detector is now exiting.

觀察以上輸出報告,可知 Block 後面的序號不僅僅針對導致記憶體泄漏的函數,這個例子中的 Block 2 應該屬於 testFun2(),但由於 testFun2() 正常釋放了記憶體,因此在輸出報告中沒有找到 Block 2,只能找到 Block 1Block 3,這個例子說明瞭輸出報告中 Block 序號不一定是連續的,可能缺失了一些序號,屬正常現象。第 40~41 行中的 79 bytes 包含有: Block 1 中申請 int4 bytes 及對應的 36 bytes 記憶體管理頭、 Block 3 中申請 char[3]3 bytes 及對應的 36 bytes 記憶體管理頭,共計 \(4 + 36 + 3 + 36 = 79 bytes\)。第 42 行中的 117 bytes 表示運行過程中一共分配的記憶體大小,計算方式與上一節一樣。

本文作者:木三百川

本文鏈接:https://www.cnblogs.com/young520/p/17259898.html

版權聲明:本文系博主原創文章,著作權歸作者所有。商業轉載請聯繫作者獲得授權,非商業轉載請附上出處鏈接。遵循 署名-非商業性使用-相同方式共用 4.0 國際版 (CC BY-NC-SA 4.0) 版權協議。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 路由模式(Router Pattern):將頁面的不同狀態映射到不同的URL路徑上,使得用戶可以直接通過URL來訪問頁面的不同狀態。 路由模式通常用於實現單頁面應用(SPA)的頁面導航和狀態管理。具體來說,路由模式通過解析URL路徑來確定應該顯示哪個頁面,並使用歷史記錄API來管理頁面狀態。 一般來 ...
  • 本文使用簡單易懂的代碼,實現了一組可以構造解析器的函數。相信通過本文的演示,你應該對解析器的基本工作原理有了一個淺淺的瞭解。 ...
  • 計算屬性模式(Computed Property Pattern):在JavaScript中,可以使用Object.defineProperty()方法來實現計算屬性模式,通過get和set方法來計算屬性值。 計算屬性模式用於將對象的某些屬性值與其他屬性值相關聯。該模式常用於Vue.js等框架中。 ...
  • 終端主要的目的是可以讓我們在文件系統中進行四處瀏覽和打開/運行某些東西,只是在 windows 上我們之前都是用 GUI 界面進行操作的,現在是在終端中使用 shell 命令進行操作而已。 ...
  • UML(Unified Modeling Language)統一建模語言 power designer中給類的“操作”添加“輸入參數” 查看所設計類的代碼: 類和類的六種關係 1 繼承關係(泛化關係) 語義:子類和父類之間的關係 語法:extends 符號:(子類指向父類) power design ...
  • 代理模式(Proxy Pattern)是一種結構型設計模式,結構型模式描述如何將類或對象按某種佈局組成更大的結構。它允許你提供一個代理對象來控制對另一個對象的訪問。代理對象擁有與實際對象相同的介面,因此它可以被用來代替實際對象。 ...
  • 1. JVM線程優化 1.1. 當空間不足時,可以調整線程使用的記憶體 1.2. 每個線程都有一個原生棧,操作系統會在這裡存儲線程的調用棧信息 1.3. 原生棧的大小是1 MB 1.3.1. 32位的Windows JVM原生棧大小是320KB 1.3.2. 在64位的JVM中,通常不會修改這個值 1 ...
  • 1. 下載jsoncpp -->https://github.com/open-source-parsers/jsoncpp/tree/update 兩種下載方法: 方法一:git clone ... 到伺服器上(或虛擬機上...),有點慢,甚至會失敗...! 方法二:下載 zip包,通過第三方軟體 ...
一周排行
    -Advertisement-
    Play Games
  • 概述:本文代碼示例演示瞭如何在WPF中使用LiveCharts庫創建動態條形圖。通過創建數據模型、ViewModel和在XAML中使用`CartesianChart`控制項,你可以輕鬆實現圖表的數據綁定和動態更新。我將通過清晰的步驟指南包括詳細的中文註釋,幫助你快速理解並應用這一功能。 先上效果: 在 ...
  • openGauss(GaussDB ) openGauss是一款全面友好開放,攜手伙伴共同打造的企業級開源關係型資料庫。openGauss採用木蘭寬鬆許可證v2發行,提供面向多核架構的極致性能、全鏈路的業務、數據安全、基於AI的調優和高效運維的能力。openGauss深度融合華為在資料庫領域多年的研 ...
  • openGauss(GaussDB ) openGauss是一款全面友好開放,攜手伙伴共同打造的企業級開源關係型資料庫。openGauss採用木蘭寬鬆許可證v2發行,提供面向多核架構的極致性能、全鏈路的業務、數據安全、基於AI的調優和高效運維的能力。openGauss深度融合華為在資料庫領域多年的研 ...
  • 概述:本示例演示了在WPF應用程式中實現多語言支持的詳細步驟。通過資源字典和數據綁定,以及使用語言管理器類,應用程式能夠在運行時動態切換語言。這種方法使得多語言支持更加靈活,便於維護,同時提供清晰的代碼結構。 在WPF中實現多語言的一種常見方法是使用資源字典和數據綁定。以下是一個詳細的步驟和示例源代 ...
  • 描述(做一個簡單的記錄): 事件(event)的本質是一個委托;(聲明一個事件: public event TestDelegate eventTest;) 委托(delegate)可以理解為一個符合某種簽名的方法類型;比如:TestDelegate委托的返回數據類型為string,參數為 int和 ...
  • 1、AOT適合場景 Aot適合工具類型的項目使用,優點禁止反編 ,第一次啟動快,業務型項目或者反射多的項目不適合用AOT AOT更新記錄: 實實在在經過實踐的AOT ORM 5.1.4.117 +支持AOT 5.1.4.123 +支持CodeFirst和非同步方法 5.1.4.129-preview1 ...
  • 總說周知,UWP 是運行在沙盒裡面的,所有許可權都有嚴格限制,和沙盒外交互也需要特殊的通道,所以從根本杜絕了 UWP 毒瘤的存在。但是實際上 UWP 只是一個應用模型,本身是沒有什麼許可權管理的,許可權管理全靠 App Container 沙盒控制,如果我們脫離了這個沙盒,UWP 就會放飛自我了。那麼有沒... ...
  • 目錄條款17:讓介面容易被正確使用,不易被誤用(Make interfaces easy to use correctly and hard to use incorrectly)限制類型和值規定能做和不能做的事提供行為一致的介面條款19:設計class猶如設計type(Treat class de ...
  • title: 從零開始:Django項目的創建與配置指南 date: 2024/5/2 18:29:33 updated: 2024/5/2 18:29:33 categories: 後端開發 tags: Django WebDev Python ORM Security Deployment Op ...
  • 1、BOM對象 BOM:Broswer object model,即瀏覽器提供我們開發者在javascript用於操作瀏覽器的對象。 1.1、window對象 視窗方法 // BOM Browser object model 瀏覽器對象模型 // js中最大的一個對象.整個瀏覽器視窗出現的所有東西都 ...