使用Visual Leak Detector检测内存泄漏

安装Visual Leak Detector

前往官网下载并安装。

设置

附加包含目录添加C:\Program Files (x86)\Visual Leak Detector\include
附加库目录添加C:\Program Files (x86)\Visual Leak Detector\lib\Win64
附加依赖项添加vld.lib
将vld.ini中的ReportTo修改为both

测试

写一段内存泄漏的代码,然后运行,程序结束后将会在调试器和文件中生成内存泄漏报告,VLD_FORCE_ENABLE宏定义是为了Release版本也能生成报告。

#include "stdafx.h"
#define VLD_FORCE_ENABLE
#include 


int _tmain(int argc, _TCHAR* argv[])
{
    VLDGlobalEnable();
    VLDReportLeaks();
    char *strTest=new char[1024];
    sprintf(strTest,"111111");
    printf(strTest);
    VLDGlobalDisable();
    return 0;
}

WARNING: Visual Leak Detector detected memory leaks!
---------- Block 57 at 0x0000000000B93180: 1024 bytes ----------
  Leak Hash: 0xD1436EBC, Count: 1, Total 1024 bytes
  Call Stack (TID 10276):
    ntdll.dll!RtlAllocateHeap()
    f:\dd\vctools\crt_bld\self_64_amd64\crt\src\new.cpp (59): LeakTest.exe!operator new() + 0x8 bytes
    d:\work\leaktest\leaktest\leaktest.cpp (14): LeakTest.exe!wmain()
    f:\dd\vctools\crt_bld\self_64_amd64\crt\src\crt0.c (278): LeakTest.exe!__tmainCRTStartup() + 0x12 bytes
    kernel32.dll!BaseThreadInitThunk() + 0xD bytes
    ntdll.dll!RtlUserThreadStart() + 0x21 bytes
  Data:
    31 31 31 31    31 31 00 BA    0D F0 AD BA    0D F0 AD BA     111111.. ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........
    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA    0D F0 AD BA     ........ ........

提醒

该工具只能检测堆(Heap)上分配的内存泄漏,不能检测VirtualAlloc(Private Data)申请的内存泄漏

你可能感兴趣的:(使用Visual Leak Detector检测内存泄漏)