当前位置: 首页 > article >正文

VS2019使用VLD(Visual Leak Detector)检测CPP内存泄漏

VS2019使用VLD(Visual Leak Detector)检测CPP内存泄漏

环境:

编译器: VS2019
VLD: 2.5.1

前言

在windows平台下,VLD(Visual Leak Detector)是一个比较准确的内存泄漏检测工具,支持输出检测报告。
下载地址:https://github.com/KindDragon/vld

1. 安装

下载路径:

https://github.com/KindDragon/vld/releases/download/v2.5.1/vld-2.5.1-setup.exe

双击安装即可

2. 编写测试代码

  • main.cpp
#include "vld.h"
int main()
{
    char *s = new char[1024];
    return 0;
}
  • CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)

project(main)

# release enable vld
add_definitions(-DVLD_FORCE_ENABLE)
# set vld include and lib
SET(VLD_PATH "C:/Program Files (x86)/Visual Leak Detector")
SET(VLD_INCLUDE "${VLD_PATH}/include")
SET(VLD_LIB "${VLD_PATH}/lib/Win64/vld.lib")

add_executable( ${PROJECT_NAME} main.cpp)
  • 编译为64位RelWithDebInfo
cmake -B bin -Thost=x64
cmake --build bin --config=RelWithDebInfo

3. 结果

main.exe
Visual Leak Detector read settings from: C:\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 0x00000000600A8B60: 1024 bytes ----------
  Leak Hash: 0xBF161843, Count: 1, Total 1024 bytes
  Call Stack (TID 15580):
    ucrtbase.dll!malloc_base()
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\heap\new_scalar.cpp (35): main.exe!operator new() + 0x8 bytes
    C:\memoryLeakTest\main.cpp (5): main.exe!main()
    D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl (288): main.exe!__scrt_common_main_seh() + 0x22 bytes
    KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
    ntdll.dll!RtlUserThreadStart() + 0x21 bytes
  Data:
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........
    00 00 00 00    00 00 00 00    00 00 00 00    00 00 00 00     ........ ........


Visual Leak Detector detected 1 memory leak (1024 bytes).
Largest number used: 1024 bytes.
Total allocations: 1024 bytes.
Visual Leak Detector is now exiting.

4. 其他

4.1 报告输出到文件

复制C:\Program Files (x86)\Visual Leak Detector\vld.ini文件到main.exe所在目录

修改vld.ini中的ReportTo = debuggerReportTo = both

然后运行main.exe会同时输出信息到控制台和memory_leak_report.txt文件

4.2 输出指定名称的报告文件

VLDSetReportOptions(VLD_OPT_REPORT_TO_FILE,L"main.txt");

http://www.kler.cn/a/7141.html

相关文章:

  • 关于 Oracle Database Express Edition 的功能和安装
  • vueRouter路由切换时实现页面子元素动画效果, 左右两侧滑入滑出效果
  • 爬取链家二手房房价数据存入mongodb并进行分析
  • 哈希表学习分享
  • C++builder中的人工智能(27):如何将 GPT-3 API 集成到 C++ 中
  • 基于Springboot+Vue的中国蛇类识别系统 (含源码数据库)
  • FPGA有哪些优质的带源码的IP开源网站?
  • ChatGPT如何写作-怎么让chatGPT批量写作
  • GPG Overview
  • 算法学习|动态规划 LeetCode 1049. 最后一块石头的重量 II、 494. 目标和、474.一和零
  • Vue3学习笔记(9.3)
  • 常用的32种设计模式
  • 记一次ios重打包签名的过程
  • Vue|计算属性
  • ATE测试工程师还有发展前景吗?薪资待遇怎么样?
  • [ctfshow 2023 愚人杯] crypto,rev,pwn
  • 【JavaWeb】3—JavaScript
  • 开发板 ping 虚拟机
  • redis源码解析(三)——dict
  • 网络优化小结
  • Matplotlib 数据绘图基础入门
  • json-chatGPT-httpstatus
  • Redis7搭建主从+哨兵通俗易懂
  • 人工智能和5G相结合会碰撞出怎样的火花?
  • EMC经典问答85问(6-8问)
  • 服务雪崩、服务限流、服务熔断和服务降级