【Visual Leak Detector】配置項 SkipHeapFreeLeaks

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

使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記。本篇介紹 VLD 配置文件中配置項 SkipHeapFreeLeaks 的使用方法。 ...


說明

使用 VLD 記憶體泄漏檢測工具輔助開發時整理的學習筆記。本篇介紹 VLD 配置文件中配置項 SkipHeapFreeLeaks 的使用方法。同系列文章目錄可見 《記憶體泄漏檢測工具》目錄

目錄


1. 配置文件使用說明

在程式中通過 #include "vld.h" 的方式檢測記憶體泄漏時,VLD 首先會嘗試在程式的生成目錄下讀取 vld.ini 文件,若未讀取成功,則會嘗試在 VLD 的安裝目錄下讀取 vld.ini 文件,若仍未讀取成功,則會使用內置的預設配置,內置的預設配置如果不動源碼是無法更改的,因此通過修改相應目錄下的 vld.ini 文件來定製 VLD 功能是最好的選擇。當配置參數等號右邊為空,或者給配置了不合法值時,在使用過程中會被程式重置到預設值。

2. 設置是否跳過堆記憶體泄漏檢測

參數名SkipHeapFreeLeaks

有效賦值yesno

預設值no

功能說明文檔原話Determines whether or not report memory leaks when missing HeapFree calls. 但是當我把這個變數設置為 yes 時仍可以檢測到堆上的記憶體泄漏,這個配置項的實際作用有待進一步研究。

2.1 測試代碼

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

void testFun(int i)
{
    int *ptr = new int(i);
    printf("ptr = %08x, *ptr = %08x.\n", ptr, *ptr);

    int *pData= (int *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(int));
    printf("pData = %08x, *pData = %08x.\n", pData, *pData);
}

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

    testFun(1);

    return a.exec();
}

測試環境:QT 5.9.2MSVC 2015 32bitDebug 模式,VLD 版本為 2.5.1,VLD 配置文件只對該參數做修改,測試工程所在路徑為:E:\Cworkspace\Qt 5.9\QtDemo\testVLD

2.2 SkipHeapFreeLeaks = no 時的輸出

標準輸出窗顯示:

ptr = 00539498, *ptr = 00000001.
pData = 00535720, *pData = 00000000.

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 0x00535720: 4 bytes ----------
  Leak Hash: 0x426C3DE0, Count: 1, Total 4 bytes
  Call Stack (TID 30052):
    ntdll.dll!RtlAllocateHeap()
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (10): testVLD.exe!testFun() + 0x11 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!main() + 0x7 bytes
    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:
    00 00 00 00                                                  ........ ........


---------- Block 1 at 0x00539498: 4 bytes ----------
  Leak Hash: 0x19BB156F, Count: 1, Total 4 bytes
  Call Stack (TID 30052):
    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 (7): testVLD.exe!testFun() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!main() + 0x7 bytes
    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:
    01 00 00 00                                                  ........ ........


Visual Leak Detector detected 2 memory leaks (44 bytes).
Largest number used: 44 bytes.
Total allocations: 44 bytes.
Visual Leak Detector is now exiting.

2.3 SkipHeapFreeLeaks = yes 時的輸出

標準輸出窗顯示:

ptr = 014e9450, *ptr = 00000001.
pData = 014e84b0, *pData = 00000000.

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 0x014E84B0: 4 bytes ----------
  Leak Hash: 0x7C37FF93, Count: 1, Total 4 bytes
  Call Stack (TID 7564):
    ntdll.dll!RtlAllocateHeap()
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (10): testVLD.exe!testFun() + 0x11 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!main() + 0x7 bytes
    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:
    00 00 00 00                                                  ........ ........


---------- Block 1 at 0x014E9450: 4 bytes ----------
  Leak Hash: 0x8876C556, Count: 1, Total 4 bytes
  Call Stack (TID 7564):
    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 (7): testVLD.exe!testFun() + 0x7 bytes
    e:\cworkspace\qt 5.9\qtdemo\testvld\main.cpp (18): testVLD.exe!main() + 0x7 bytes
    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:
    01 00 00 00                                                  ........ ........


Visual Leak Detector detected 2 memory leaks (44 bytes).
Largest number used: 44 bytes.
Total allocations: 44 bytes.
Visual Leak Detector is now exiting.

2.4 輸出結果對比

SkipHeapFreeLeaks = noSkipHeapFreeLeaks = yes 的輸出結果似乎沒有什麼實質性的差異,不過有一點需註意,使用 new 分配堆記憶體時,會額外添加 36 bytes 大小的記憶體用於追蹤管理,但使用 HeapAlloc() 函數分配堆記憶體時,則沒有額外的記憶體使用,因此上述測試例的 Total allocations44 bytes,而不是 80 bytes

本文作者:木三百川

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

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


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

-Advertisement-
Play Games
更多相關文章
  • 本博文介紹CSS中的基礎選擇器和複合選擇器。基礎選擇器包括標簽選擇器、類選擇器、id選擇器和通配符選擇器,複合選擇器包括後代選擇器、子選擇器、並集選擇器和偽類選擇器。 ...
  • 在上篇隨筆《基於Admin.NET框架的前端的一些改進和代碼生成處理(1)》中大致介紹了一些關於對Admin.NET框架的前端的改造工作,主要目的就是希望能夠增加代碼的簡潔和可讀性,以及利用代碼生成工具來快速生成相關的代碼,從而減少開發過程中的繁瑣問題。本篇隨筆繼續探討一下,對其中一些模塊功能進行一... ...
  • Array --JavaScript內置對象 描述 可以用一個變數存儲多種數據類型的Array對象,Array不是關聯數組,不能使用字元串作為索引訪問數組元素,需要使用非負整數的下標訪問數組中的元素。 和對象的某些特征很相似,例如:屬性訪問器一半相似,衍生出的使用 .call() 或者 .apply ...
  • 享元模式(Flyweight Pattern):是一種用於優化對象創建和管理的設計模式。它旨在減少記憶體消耗和提高性能,通過共用具有相同狀態的對象來實現這一目標。 具體來說,享元模式涉及兩個主要的對象:享元工廠和具有共用狀態的享元對象。享元工廠負責創建和管理共用對象,以確保每個對象只被創建一次。享元對 ...
  • 上一篇文章我們介紹了 Vue2模版編譯原理,這一章我們的目標是弄清楚模版 template和響應式數據是如何渲染成最終的DOM。數據更新驅動視圖變化這部分後期會單獨講解 我們先看一下模版和響應式數據是如何渲染成最終DOM 的流程 Vue初始化 new Vue發生了什麼 Vue入口構造函數 funct ...
  • 定義 觀察者模式屬於行為型模式,它定義了對象間的一種一對多的依賴關係,當一個對象的狀態發生改變時,所有依賴於它的對象都將得到通知,並自動更新。 一種一對多的關係中一稱為被觀察者也叫目標對象Subject而多則稱為觀察者對象Observer 觀察者模式中通常有兩個模型,一個觀察者(observer)和 ...
  • UML 類圖 1 類圖的結構 用動物園的類圖結構來舉例,先抽象化動物類如圖所示: 一個類圖包括類名、屬性和行為,類名不用解釋,在介紹屬性和行為前,先瞭解一下訪問許可權: ‘ - ’ private:只有類內部的成員才能訪問 ‘ + ’ public:類內部和類外部都能訪問 ‘ # ’ protecte ...
  • XSS攻擊是什麼? XSS攻擊是指攻擊者利用網站中的漏洞,向頁面中註入惡意腳本,從而獲取用戶的信息或者控制用戶的電腦。 舉一個通俗的例子,早期使用JSP頁面渲染頁面的項目,如果將用戶名改成nick<alert>1</alert>,則當用戶打開頁面時,就會彈出一個警告框,而這個警告框可以被惡意腳本所 ...
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...