VLD
代码
Release版本下检测内存泄漏配置
#define VLD_FORCE_ENABLE
#include <vld.h>
#pragma comment(lib, "vld.lib")
int main()
{
VLDGlobalEnable();
VLDReportLeaks();
int* p = new int;
VLDGlobalDisable();
}
结果
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x00000000DC071C60: 4 bytes ----------
Leak Hash: 0x14FC14D1, Count: 1, Total 4 bytes
Call Stack (TID 181776):
ucrtbase.dll!malloc_base()
D:\a01\_work\26\s\src\vctools\crt\vcstartup\src\heap\new_scalar.cpp (35): ConsoleApplication4.exe!operator new() + 0x8 bytes
C:\Users\Nova001845\source\repos\ConsoleApplication4\ConsoleApplication4\ConsoleApplication4.cpp (64): ConsoleApplication4.exe!main()
D
Dr. Memory
配置
对于 Visual Studio,选择Tools | External Tools...菜单选项以打开“外部工具”对话框。单击添加并填写如下字段(drmemory.exe如果您将其安装在其他位置,请调整路径)
Title: Dr. Memory
Command: C:\Program Files (x86)\Dr. Memory\bin\drmemory.exe
Arguments: -visual_studio -- $(TargetPath)
Initial Directory: $(TargetDir)
代码
int main()
{
int* p = new int;
}
运行
现在可以选择Tools | Dr. Memory,Visual Studio 将运行应用程序。Dr. Memory 的输出(连同程序)将打印到输出窗口。可以双击任何调用堆栈框架上的源文件,以便自动打开该文件到指定的行号。
结果
Dr. Memory version 2.5.0
Running "C:\Users\Nova001845\source\repos\ConsoleApplication4\x64\Release\ConsoleApplication4.exe"
Using system call file C:\Users\Nova001845\Downloads\DrMemory-Windows-2.5.0\DrMemory-Windows-2.5.0\drmemory\logs\symcache\syscalls_x64.txt
Error #1: LEAK 4 bytes
replace_operator_new
d:\a\drmemory\drmemory\common\alloc_replace.c(2903):
main
C:\Users\Nova0018