【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
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...