说明
使用 VLD 内存泄漏检测工具辅助开发时整理的学习笔记。本篇主要介绍 VLD 库提供的 22 个外部接口。同系列文章目录可见 《内存泄漏检测工具》目录
- 说明
- 1. 头文件简介
- 2. 文件 vld_def.h 简介
- 3. 文件 vld.h 简介
- 3.1 接口 VLDDisable
- 3.2 接口 VLDEnable
- 3.3 接口 VLDRestore
- 3.4 接口 VLDGlobalDisable
- 3.5 接口 VLDGlobalEnable
- 3.6 接口 VLDReportLeaks
- 3.7 接口 VLDReportThreadLeaks
- 3.8 接口 VLDGetLeaksCount
- 3.9 接口 VLDGetThreadLeaksCount
- 3.10 接口 VLDMarkAllLeaksAsReported
- 3.11 接口 VLDMarkThreadLeaksAsReported
- 3.12 接口 VLDRefreshModules
- 3.13 接口 VLDEnableModule
- 3.14 接口 VLDDisableModule
- 3.15 接口 VLDGetOptions
- 3.16 接口 VLDGetReportFilename
- 3.17 接口 VLDSetOptions
- 3.18 接口 VLDSetModulesList
- 3.19 接口 VLDGetModulesList
- 3.20 接口 VLDSetReportOptions
- 3.21 接口 VLDSetReportHook
- 3.22 接口 VLDResolveCallstacks
- 4. 接口使用思路
1. 头文件简介
VLD 2.5.1 安装 完成后,安装目录的 include
文件夹下有两个头文件:vld.h
和 vld_def.h
,其中 vld.h
文件会 #include "vld_def.h"
,因此在实际使用时,项目中要同时添加这两个头文件(或将这两个文件放在编译器的搜索路径中),但只需包含 vld.h
文件。
2. 文件 vld_def.h 简介
这个文件里主要以宏的形式定义了 15 个 VLD 配置项的掩码,这 15 个配置项与 vld.ini
配置文件中的 14 个配置项不是完全对应的,将这些配置项掩码与 vld.h
文件中的接口结合起来用,可以实现在运行过程中对 VLD 的配置进行动态修改。其中 9 个配置项可作为接口 VLDSetOptions
的输入,另外 4 个配置项可作为接口 VLDSetReportOptions
的输入,剩余的 2 个配置项分别是 VLD_OPT_SELF_TEST
与 VLD_OPT_VLDOFF
,其中 VLD_OPT_SELF_TEST
只能通过修改 vld.ini
文件中的 SelfTest
进行设置(详见 配置项 SelfTest),VLD_OPT_VLDOFF
只能通过修改 vld.ini
文件中的 VLD
进行设置(详见 配置项 VLD),当 VLD_OPT_VLDOFF
被设置后,所有接口也都会变得不可用。
#define VLD_OPT_AGGREGATE_DUPLICATES 0x0001 // If set, aggregate duplicate leaks in the leak report.
#define VLD_OPT_MODULE_LIST_INCLUDE 0x0002 // If set, modules in the module list are included, all others are excluded.
#define VLD_OPT_REPORT_TO_DEBUGGER 0x0004 // If set, the memory leak report is sent to the debugger.
#define VLD_OPT_REPORT_TO_FILE 0x0008 // If set, the memory leak report is sent to a file.
#define VLD_OPT_SAFE_STACK_WALK 0x0010 // If set, the stack is walked using the "safe" method (StackWalk64).
#define VLD_OPT_SELF_TEST 0x0020 // If set, perform a self-test to verify memory leak self-checking.
#define VLD_OPT_SLOW_DEBUGGER_DUMP 0x0040 // If set, inserts a slight delay between sending output to the debugger.
#define VLD_OPT_START_DISABLED 0x0080 // If set, memory leak detection will initially disabled.
#define VLD_OPT_TRACE_INTERNAL_FRAMES 0x0100 // If set, include useless frames (e.g. internal to VLD) in call stacks.
#define VLD_OPT_UNICODE_REPORT 0x0200 // If set, the leak report will be encoded UTF-16 instead of ASCII.
#define VLD_OPT_VLDOFF 0x0400 // If set, VLD will be completely deactivated. It will not attach to any modules.
#define VLD_OPT_REPORT_TO_STDOUT 0x0800 // If set, the memory leak report is sent to stdout.
#define VLD_OPT_SKIP_HEAPFREE_LEAKS 0x1000 // If set, VLD skip HeapFree memory leaks.
#define VLD_OPT_VALIDATE_HEAPFREE 0x2000 // If set, VLD verifies and reports heap consistency for HeapFree calls.
#define VLD_OPT_SKIP_CRTSTARTUP_LEAKS 0x4000 // If set, VLD skip crt srtartup memory leaks.
3. 文件 vld.h 简介
这个文件里主要声明了 VLD 的 22 个外部接口。文件前面的编译条件 defined _DEBUG || defined VLD_FORCE_ENABLE
表明 VLD 通常只能在 DEBUG
模式下运行,若要在 RELEASE
模式下运行,可以在包含头文件 vld.h
前预先定义 VLD_FORCE_ENABLE
宏。一个有趣的现象是,这个 _DEBUG
宏有时候在 RELEASE
模式下也会被定义,参考文章 神秘的 _DEBUG 宏从何处来?。接口中的一些类型别名定义如下:
typedef int VLD_BOOL;
typedef unsigned int VLD_UINT;
typedef size_t VLD_SIZET;
typedef void* VLD_HMODULE;
3.1 接口 VLDDisable
该函数用于禁用当前线程的内存泄漏检测功能。
// VLDDisable - Disables Visual Leak Detector's memory leak detection at
// runtime. If memory l