vs2019使用vld检测内存泄漏

引言

在windows平台下,一个检测内存泄漏的工具是vld。
下载地址是:https://kinddragon.github.io/vld/
github地址:https://github.com/KindDragon/vld

使用

从网上下载exe安装包。安装过程中,可以选择把vld头文件、lib、dll安装在系统环境下。
安装完成之后,默认在C:Program Files (x86)Visual Leak Detector目录有头文件和x86、x64平台debug版本的库。vld只能在debug模式下使用。
然后,在项目中包含vld.h头文件。
当前,vld最高支持到vs2015,在vs2019默认没有提示泄漏出现在哪一行。我们可以在属性设置中做相关配置即可,如下图:

配置

默认使用安装目录下的vld.ini配置文件。我们可以把该文件放在运行程序同一个目录下,修改这个文件。
比如,我们可以指定将输出信息保存到文件中,同时打印在屏幕上,然后配置文件的路径:

; Sets the report file destination, if reporting to file is enabled. A relative
; path may be specified and is considered relative to the process' working
; directory.
;
;   Valid Values: Any valid path and filename.
;   Default: .memory_leak_report.txt
;
ReportFile = .memory_leak_report.txt

; Sets the report destination to either a file, the debugger, or both. If
; reporting to file is enabled, the report is sent to the file specified by the
; ReportFile option.
;
;   Valid Values: debugger, file, both
;   Default: debugger
;
ReportTo = both

示例

如下一段代码:

#include <iostream>
#include <vld.h>

int main()
{
	int* a = new int[20];

	return 0;
}

运行结果:

原文地址:https://www.cnblogs.com/zuofaqi/p/13795862.html