MFC——内存泄露检测

VLD为VC++/VS下的内存泄露检测工具 
1、首先下载安装vld,直接下载安装包,安装过程中会直接添加环境变量。 
2、在安装目录下有vld.h, vldapi.h, vld.lib, vldmt.lib, vldmtdll.lib, dbghelp.dll等文件,在使用的时候在附加包含附录中添加inlude目录或者将.h文件拷贝到工程默认的include目录下 
这里写图片描述
,在附加库目录中添加vld安装目录下的lib/Win32或者lib/Win64库目录或者将相应库目录下的lib文件复制到工程默认的lib库目录下即可。 
这里写图片描述

3、在使用的时候,在相应的文件中包含vld.h头文件,并添加条件编译语句,则在Debug下,如果有内存泄露将会有相应提示。

#ifdef _DEBUG
#include <vld.h>
#endif // DEBUG
#include <iostream>
#include <windows.h>
using namespace std;
int main(int argc, char** argv)
{
    char* vldPtr = new char;
    char* vldPtr2 = new char[100];
    Sleep(1000);
    //delete vldPtr;
    //delete[]vldPtr2;
    return 0;
}

调试运行,将会出现相应的内存泄露信息,双击发生内存泄露信息中的定位信息,会自动跳到发生内存泄露的语句,如下面的: 
e:\data\project\myofferinterviewquestions\visualleakdebugtest\vldtest.cpp (13): VisualLeakDebugTest.exe!main() + 0x7 bytes 
以及 
e:\data\project\myofferinterviewquestions\visualleakdebugtest\vldtest.cpp (14): VisualLeakDebugTest.exe!main() + 0x7 bytes

Visual Leak Detector read settings from: E:\data\C++\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x006F9998: 1 bytes ----------
  Leak Hash: 0x877EEED5, Count: 1, Total 1 bytes
  Call Stack (TID 3956):
    MSVCR120D.dll!operator new()
    e:\data\project\myofferinterviewquestions\visualleakdebugtest\vldtest.cpp (13): VisualLeakDebugTest.exe!main() + 0x7 bytes
    f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): VisualLeakDebugTest.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!RtlInitializeExceptionChain() + 0x8F bytes
    ntdll.dll!RtlInitializeExceptionChain() + 0x5A bytes
  Data:
    CD                                                           ........ ........
---------- Block 2 at 0x00704208: 100 bytes ----------
  Leak Hash: 0xE714C3E0, Count: 1, Total 100 bytes
  Call Stack (TID 3956):
    MSVCR120D.dll!operator new()
    f:\dd\vctools\crt\crtw32\stdcpp\newaop.cpp (6): VisualLeakDebugTest.exe!operator new[]() + 0x9 bytes
    e:\data\project\myofferinterviewquestions\visualleakdebugtest\vldtest.cpp (14): VisualLeakDebugTest.exe!main() + 0x7 bytes
    f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): VisualLeakDebugTest.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x24 bytes
    ntdll.dll!RtlInitializeExceptionChain() + 0x8F bytes
    ntdll.dll!RtlInitializeExceptionChain() + 0x5A bytes
  Data:
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
    CD CD CD CD    CD CD CD CD    CD CD CD CD    CD CD CD CD     ........ ........
    CD CD CD CD                                                  ........ ........
Visual Leak Detector detected 2 memory leaks (173 bytes).
Largest number used: 173 bytes.
Total allocations: 173 bytes.
Visual Leak Detector is now exiting.
程序“[15164] VisualLeakDebugTest.exe”已退出,返回值为 0 (0x0)。

如果没有发生内存泄露,那么返回;

Visual Leak Detector read settings from: E:\data\C++\Visual Leak Detector\vld.ini
Visual Leak Detector Version 2.5 installed.
No memory leaks detected.
Visual Leak Detector is now exiting.
程序“[3468] VisualLeakDebugTest.exe”已退出,返回值为 0 (0x0)。

猜你喜欢

转载自blog.csdn.net/perfectguyipeng/article/details/79933409
今日推荐