1. 程式人生 > >使用Visual Leak Detector for Visual C++ 捕捉記憶體洩露---Visual C++記憶體洩露檢測—VLD工具使用說明

使用Visual Leak Detector for Visual C++ 捕捉記憶體洩露---Visual C++記憶體洩露檢測—VLD工具使用說明

以下經過測試:

使用Visual Leak Detector for Visual C++ 捕捉記憶體洩露
1、下載vlc

2、建立應用並配置

c/c++ -> General -> Additional Include Directories = C:\Program Files (x86)\Visual Leak Detector\include

Linker -> General -> Additional Library Directories = C:\Program Files (x86)\Visual Leak Detector\lib\Win32

3、捕捉內在洩露

複製程式碼
#include "stdafx.h"

#ifdef _DEBUG  
#include "vld.h"
#endif

int _tmain(int argc, _TCHAR* argv[])
{
    char* c1 = new char[100];
    char* c2  =(char*)malloc(200);
    char c3[1000];

    //delete[] c1;
    //free(c2);

    return 0;
}

錯誤:#error : [VLD COMPILE ERROR] '#include <vld.h>' should appear before '#include <afxwin.h>' in file stdafx.h

解決辦法:

在stdafx.h有#include <afxwin.h>

將#include <vld.h>放在#include <afxwin.h>之前,可以解決問題
複製程式碼
輸入如下:

複製程式碼
Visual Leak Detector Version 2.2.3 installed.
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x008CD6F8: 100 bytes ----------
  Call Stack:
    c:\users\enost_000\desktop\consoleapplication1\consoleapplication1\consoleapplication1.cpp (12): ConsoleApplication1.exe!wmain + 0x7 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ConsoleApplication1.exe!__tmainCRTStartup + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ConsoleApplication1.exe!wmainCRTStartup
    0x74EE8543 (File and line number not available): KERNEL32.DLL!BaseThreadInitThunk + 0xE bytes
    0x770EAC69 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x85 bytes
    0x770EAC3C (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x58 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                                                  ........ ........


---------- Block 2 at 0x008CD798: 200 bytes ----------
  Call Stack:
    c:\users\enost_000\desktop\consoleapplication1\consoleapplication1\consoleapplication1.cpp (13): ConsoleApplication1.exe!wmain + 0xD bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (533): ConsoleApplication1.exe!__tmainCRTStartup + 0x19 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (377): ConsoleApplication1.exe!wmainCRTStartup
    0x74EE8543 (File and line number not available): KERNEL32.DLL!BaseThreadInitThunk + 0xE bytes
    0x770EAC69 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x85 bytes
    0x770EAC3C (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x58 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    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 (372 bytes).
Largest number used: 372 bytes.
Total allocations: 372 bytes.
Visual Leak Detector is now exiting.
The program '[0x262C] ConsoleApplication1.exe' has exited with code 0 (0x0).
複製程式碼
 

4、qt也vlc
  vlc只支援vc編譯器,所以 qt的話只能夠使用vc編譯器捕捉並解決記憶體洩露之後,再考慮使用mingw(gcc/g++)編譯程式。

以下可以做參考,本人未測試過:

from:

Visual C++記憶體洩露檢測VLD工具使用說明

一.VLD工具概述

Visual Leak DetectorVLD)是一款用於Visual C++的免費的記憶體洩露檢測工具。他的特點有:可以得到記憶體洩漏點的呼叫堆疊,如果可以的話,還可以得到其所在檔案及行號;可以得到洩露記憶體的完整資料;可以設定記憶體洩露報告的級別;並且是開源免費的。

二.VLD下載

本文後附有vld1.0的工具包,下載解包後就可使用。

三.VLD安裝

方法一:

解壓之後得到vld.h, vldapi.h, vld.lib, vldmt.lib, vldmtdll.lib, dbghelp.dll等檔案。將.h檔案拷貝到Visual C++的預設include目錄下,將.lib檔案拷貝到Visual C++的預設lib目錄下,將dbghelp.dll拷貝到你的程式的執行目錄下,便安裝完成了

方法二:

解壓之後得到vld.h, vldapi.h, vld.lib, vldmt.lib, vldmtdll.lib, dbghelp.dll等檔案。將.h檔案和.lib檔案拷貝到你要檢測的工程檔案所在的目錄裡(只針對此工程),將dbghelp.dll拷貝到你的程式的執行目錄下。就完成安裝了。

四.VLD使用

在包含入口函式的.cpp檔案中包含vld.h就可以了。下面以一個例子進行說明(源程式見附錄)

1.加入標頭檔案:

2.編譯:

3.debug方式下執行:檢視VC的輸出資訊:

記憶體洩露所在的位置

4檢視VC輸出資訊:

 "WARNING: Visual Leak Detector detected meory leaks!"

5. 如果沒有記憶體洩露,此輸出的資訊為:

      "No memory leaks detected"

五.附錄

1.測試用檔案

#include"vld.h"

#include"iostream.h"

#include"stdio.h"

#include"stdlib.h"

#include"string.h"

voidFunction1(char*p)

{

char *pTmp = new char[255];

memset(pTmp, 0x0, 255);

strncpy(pTmp,p, 255);

//delete pTmp;

}

intFunction2(void)

{

characString[] ="this is test!";

       Function1(acString);

return1;

}

voidFunction3(void)

{

       Function2();

}

intmain(void)

{

cout<<"begin.............."<<endl;

       Function3();

cout<<"end................"<<endl;

return1;

}

2Vld1.0工具包

3.關於linux的記憶體洩露,可以使用valgrind工具進行檢測。

相關推薦

使用Visual Leak Detector for Visual C++ 捕捉記憶體洩露---Visual C++記憶體洩露檢測VLD工具使用說明

以下經過測試: 使用Visual Leak Detector for Visual C++ 捕捉記憶體洩露 1、下載vlc 2、建立應用並配置 c/c++ -> General -> Additional Include Directories = C:\P

在windows下使用 Visual Leak Detector for Visual C++ 2008的安裝和配置

在windows下使用 Visual Leak Detector for Visual C++ 2008的安裝和配置   3、安裝配置vld-2.2.3-setup: 可以去環境變數裡面檢查一下,如果沒有新增就手動新增下(安裝程式預設自動新增)。 一直點next。   接下

Visual Leak Detector 2.2.3 Visual C++記憶體檢測工具

         Visual Leak Detector是一款免費的、健全的、開源的Visual C++記憶體洩露檢測系統。相比Visual C++自帶的記憶體檢測機制,Visual Leak De

記憶體洩露除錯之 visual leak detector 工具

內​存​洩​露​調​試​之​ ​v​i​s​u​a​l​ ​l​e​a​k​ ​d​e​t​e​c​t​o​r​ ​工​具【轉】 本文參考此文:http://kangzj.net/visual-leak-detector-download/   另外一種檢查記憶體洩露的工具:

Windows和Linux記憶體檢測工具:Valgrind,Visual Leak Detector,CppCheck, Cpplint

1 Linux記憶體洩漏檢測工具ValgrindValgrind簡介        Valgrind是一套Linux下,開放原始碼(GPL V2)的模擬除錯工具的集合。Valgrind由核心(core)以及基於核心的其他除錯工具組成。核心類似於一個框架(framework),

vs2008 使用Visual Leak Detector檢測記憶體洩漏

http://hi.baidu.com/maydaygmail/item/8ea6ebef87ca9103560f1dfe 靈活自由是C/C++語言的一大特色,而這也為C/C++程 序員出了一個難題。當程式越來越複雜時,記憶體的管理也會變得越加複雜,稍有不慎就會

vc6.0記憶體洩露檢測工具Visual Leak Detector

如果僅僅是使用的話,無需看下面文章,只需做以下三步,當然先下載visual leak detector包,解壓後: (1)把vld.h vldapi.h拷貝到vc安裝目錄下的include資料夾下 (2)把lib檔案拷貝到vc安裝目錄下的lib資料夾下 (3)需要測試的

實操VS2015配置記憶體洩漏檢查工具Visual Leak Detector

Visual Leak Detector Version 2.5.1 installed.     Outputting the report to the debugger and to C:\Users\bm00076\Documents\Visual Studio 2015\Projects\Conso

Visual Leak Detector 2 2 3 Visual C++內存檢測工具

style 段子 內存泄露 led 庫文件 tex lan center 2.3 Visual Leak Detector是一款免費的、健全的、開源的Visual C++內存泄露檢測系統。相比Visual C++自帶的內存檢測機制,Visual Lea

VS2012下Visual Leak Detector(vld)的配置和使用

 Windows 7系統下 Visual Leak Detector 的配置和使用 1,概述 Visual Leak Detector(簡稱VLD)是一款用於Visual C++的免費的記憶體洩露檢測工具。他的特點有:可以得到記憶體洩漏點的呼叫堆疊,如果可以的話,還可

Visual Studio 2017使用Visual Leak Detector(vld)不顯示行號的設定

在安裝vld的時候,其中一步顯示只支援到vs 2015,很可惜作者沒有更新了 但是在vs 2017使用vld的時候,不會顯示行號,麻煩了,打到記憶體洩露卻不知道行號 原來是可以通過設定顯示的,方法如下: 專案屬性->連結器->所有選項->生成除錯資訊:

Visual C++記憶體洩露檢測VLD工具使用說明d

轉自:http://www.cnblogs.com/lidabo/archive/2012/07/19/2599745.html http://blog.csdn.net/zlQQhs/article/details/10039179 1.VLD工具概述 Visual

vld(Visual Leak Detector) 處理機制與使用詳解

   靈活自由是C/C++語言的一大特色,而這也為C/C++程式設計師出了一個難題。當程式越來越複雜時,記憶體的管理也會變得越加複雜,稍有不慎就會出現記憶體問題。記憶體洩漏是最常見的記憶體問題之一。記憶體洩漏如果不是很嚴重,在短時間內對程式不會有太大的影響,這也使得記憶體洩漏問題有很強的隱蔽性,不容易被發現

VS2017 編譯 Visual Leak Detector + VLD 使用示例

起因 一個Qt5+VS2017的工程,需要進行串列埠操作,在自動時發現一段時間軟體崩潰了,沒有儲存log,在 debug 的時候發現每執行一次應用佔據的記憶體就多一點,後來意識到是記憶體洩漏了。這個真是頭疼,變數太多,不知道從哪裡查詢記憶體洩漏好。於是迫切的需要找到一種追查記憶體洩漏的工具。 一開始很自然的是

Chapter 5 : More about Variables - Note for BEGINNING C#7 Programming with Visual Studio 2017.pdf

implicit conversion The implicit conversion rule for these types is this: Any type A whose range of possible values completely fits inside the ran

chapter 4 : Flow Control - Note for BEGINNING C#7 Programming with Visual Studio 2017.pdf

Boolean Assignment Operators OPERATOR CATEGORY EXAMPLE EXPRESSION RESULT &= Binary

Chapter2 Visual Studio-Note for BEGINNING C# 7 Programming with Visual Stud

visual studio 重新設定開發環境 工具->獲取工具和功能(T)… 獲取工具和功能(語言包中包括中文簡體、英文等) reset the settings to Visual C#,Visual C++,Web development…

Chapter 1 Introduction C#-Note for BEGINNING C# 7 Programming with Visual Studio 2017

Devlopment Enviroment C# 7 .NET 4.7 Visual Studio Community 2017 SQL Server Express What the .NET Framework is ? The .NE

Visual Studio 2017 for Mac

新版本 ont print eno web hidden contain visual upd Visual Studio 2017 for Mac Last Update: 2017/6/16 我們非常榮幸地宣布 Visual Studio 2017

Visual Studio Emulator for Android 折騰記

多個 develop 新版本 代理 option key 折騰 最新 mac 想用虛擬機調試Android項目,於是想到了MS免費提供的 Visual Studio Emulator for Android,這玩意價格免費量又足,N久之前試用過,速度杠杠的! 安裝包很小,