轉:VS2008內存泄露檢測

轉:http://lsshappy007.blog.163.com/blog/static/35820251201078103348430/

C++程序的複雜性很大一部分在於他的內存管理,沒有C#那樣的垃圾回收機制,內存管理對初學者來說很困難。經常會出現內存泄露的情況。那麼我們寫程序如何避免內存泄露呢?首先我們需要知道程序有沒有內存泄露,然後定位到底是哪行代碼出現內存泄露了,這樣才能將其修復。
    本文描述瞭如何檢測內存泄露。最主要的是純C,C++的程序如何檢測內存泄露。
現在有很多專業的檢測工具,比如比較有名的BoundsCheck, 但是這類工具也有他的缺點,我認爲首先BoundsCheck是商業軟件,呵呵。然後呢需要安裝,使用起來不太方便。因爲我們檢測的時候不一定經常會啓動他來檢測。這樣經常會積累很多問題,那時要解決就麻煩了。最好就是從開始編碼,一步一步的都能隨時提醒我們內存泄露。我們編程序會經常調試,假如能在每次調試程序的時候都能自動檢測內存泄露就好了。

   一. 在 MFC 中檢測內存泄漏
假如是用MFC的程序的話,很簡單。默認的就有內存泄露檢測的功能。
我們用VS2005生成了一個MFC的對話框的程序,發現他可以自動的檢測內存泄露.不用我們做任何特殊的操作. 仔細觀察,發現在每個CPP文件中,都有下面的代碼:

#ifdef _DEBUG

#define new DEBUG_NEW

#endif

DEBUG_NEW 這個宏定義在afx.h文件中,就是它幫助我們定位內存泄漏。
     在含有以上代碼的cpp文件中分配內存後假如沒有刪除,那麼停止程序的時候,VisualStudio的Output窗口就會顯示如下的信息了:

Detected memory leaks!

Dumping objects ->

d:\code\mfctest\mfctest.cpp(80) : {157} normal block at 0x003AF170, 4 bytes long.

Data: < > 00 00 00 00

Object dump complete.

    在Output窗口雙擊粗體字那一行,那麼IDE就會打開該文件,定位到該行,很容易看出是哪出現了內存泄露。

二.檢測純C++的程序內存泄露

    我試了下用VisualStudio建立的Win32 Console Application和Win32 Project項目,結果都不能檢測出內存泄露。
下面一步一步來把程序的內存泄露檢測的機制建立起來。
首先,我們需要知道C運行庫的Debug版本提供了許多檢測功能,使得我們更容易的Debug程序。在MSDN中有專門的章節講這個,叫做Debug Routines,建議大家先看看裏面的內容吧。
我們會用到裏面很重要的幾個函數。其中最重要的是 _CrtDumpMemoryLeaks();自己看MSDN裏的幫助吧。使用這個函數,需要包含頭文件crtdbg.h
該函數只在Debug版本纔有用,當在調試器下運行程序時,_CrtDumpMemoryLeaks 將在“Output(輸出)”窗口中顯示內存泄漏信息.寫段代碼試驗一下吧,如下:
檢測內存泄露版本一:

#include "stdafx.h"

#include <crtdbg.h>

int _tmain(int argc, _TCHAR* argv[])

{

    int* p = new int();

     _CrtDumpMemoryLeaks();

    return 0;

}

運行後,在Output(輸出)窗口,顯示瞭如下的信息:

Detected memory leaks!

Dumping objects ->

{112} normal block at 0x003AA770, 4 bytes long.

Data: <     > 00 00 00 00

Object dump complete.

    但是這個只是告訴我們程序有內存泄露,到底在哪泄露了一眼看不出來啊。
看我們的檢測內存泄露版本二:

#include "stdafx.h"

#ifdef _DEBUG

#define DEBUG_CLIENTBLOCK    new( _CLIENT_BLOCK, __FILE__, __LINE__)

#else

#define DEBUG_CLIENTBLOCK

#endif

#define _CRTDBG_MAP_ALLOC

#include <crtdbg.h>

#ifdef _DEBUG

#define new DEBUG_CLIENTBLOCK

#endif

int _tmain(int argc, _TCHAR* argv[])

{

    int* p = new int();

     _CrtDumpMemoryLeaks();

    return 0;

}

    該程序定義了幾個宏,通過宏將Debug版本下的new給替換了,新的new記錄下了調用new時的文件名和代碼行.運行後,可以看到如下的結果:

Detected memory leaks!

Dumping objects ->

d:\code\consoletest\consoletest.cpp(21) : {112} client block at 0x003A38B0, subtype 0, 4 bytes long.

Data: <     > 00 00 00 00

Object dump complete.

呵呵,已經和MFC程序的效果一樣了,但是等一等。看下如下的代碼吧:

int _tmain(int argc, _TCHAR* argv[])

{

    int* p = new int();

     _CrtDumpMemoryLeaks();

    delete p;

    return 0;

}

    運行後可以發現我們刪除了指針,但是它仍然報內存泄露。所以可以想象,每調用一次new,程序內部都會將該調用記錄下來,類似於有個數組記錄,假如 delete了,那麼就將其從數組中刪除,而_CrtDumpMemoryLeaks()就是把這個數組當前的狀態打印出來。
所以除了在必要的時候Dump出內存信息外,最重要的就是在程序退出的時候需要掉用一次_CrtDumpMemoryLeaks();
假如程序有不止一個出口,那麼我們就需要在多個地方都調用該函數。
更進一步,假如程序在類的析構函數裏刪除指針,怎麼辦?例如:

#include "stdafx.h"

#ifdef _DEBUG

#define DEBUG_CLIENTBLOCK    new( _CLIENT_BLOCK, __FILE__, __LINE__)

#else

#define DEBUG_CLIENTBLOCK

#endif

#define _CRTDBG_MAP_ALLOC

#include <crtdbg.h>

#ifdef _DEBUG

#define new DEBUG_CLIENTBLOCK

#endif

class Test

{

public:

     Test()       {    _p = new int();      }

     ~Test()      {   delete _p;           }

    int* _p;

};

int _tmain(int argc, _TCHAR* argv[])

{

    int* p = new int();

    delete p;

     Test t;

     _CrtDumpMemoryLeaks();

    return 0;

}

     可以看到析構函數在程序退出的時候才調用,明明沒有內存泄露,但是這樣的寫法還是報了。
如何改進呢,看檢測內存泄露版本三:

#include "stdafx.h"

#ifdef _DEBUG

#define DEBUG_CLIENTBLOCK    new( _CLIENT_BLOCK, __FILE__, __LINE__)

#else

#define DEBUG_CLIENTBLOCK

#endif

#define _CRTDBG_MAP_ALLOC

#include <crtdbg.h>

#ifdef _DEBUG

#define new DEBUG_CLIENTBLOCK

#endif

class Test

{

public:

     Test()       {    _p = new int();      }

     ~Test()      {   delete _p;           }

    int* _p;

};

int _tmain(int argc, _TCHAR* argv[])

{

     _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

    int* p = new int();

    delete p;

     Test t;

    return 0;

}

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
該語句在程序退出時自動調用 _CrtDumpMemoryLeaks。必須同時設置 _CRTDBG_ALLOC_MEM_DF 和 _CRTDBG_LEAK_CHECK_DF.
這樣,該版本已經達到了MFC一樣的效果了,但是我覺得光這樣還不夠,因爲我們只是在Output窗口中輸出信息,對開發人員的提醒還不明顯,經常會被遺漏,而且很多人就算髮現了內存泄露,但是不好修復,不會嚴重影響到程序外在表現,都不會修復。怎麼樣能讓開發人員主動的修復內存泄露的問題呢?記得曾經和人配合寫程序,我的函數參數有要求,不能爲空,但是別人老是傳空值,沒辦法了,只好在函數開始驗證函數參數,給他assert住,這樣程序運行時老是不停的彈出assert,調試程序那個煩壓,最後其他程序員煩了,就把這個問題給改好了,輸入參數就正確了。所以我覺得咱要讓程序員主動去做一件事,首先要讓他覺得做這個事是能減輕自己負擔,讓自己工作輕鬆的。呵呵,那咱們也這樣,當程序退出時,檢測到內存泄露就讓程序提示出來。
看檢測內存泄露版本四:

#include "stdafx.h"

#include <assert.h>

#ifdef _DEBUG

#define DEBUG_CLIENTBLOCK    new( _CLIENT_BLOCK, __FILE__, __LINE__)

#else

#define DEBUG_CLIENTBLOCK

#endif

#define _CRTDBG_MAP_ALLOC

#include <crtdbg.h>

#ifdef _DEBUG

#define new DEBUG_CLIENTBLOCK

#endif

void Exit()

{

    int i = _CrtDumpMemoryLeaks();

     assert( i == 0);

}

int _tmain(int argc, _TCHAR* argv[])

{

     atexit(Exit);

    int* p = new int();

    return 0;

}

    該版本會在程序退出時檢查內存泄露,假如存在就會彈出提示對話框.
atexit(Exit);設置了在程序退出時執行Exit()函數。
Exit()函數中,假如存在內存泄露,_CrtDumpMemoryLeaks()會返回非0值,就會被assert住了。
    到這個版本已經達到可以使用的程度了。但是我們還可以做些改進,因爲真要準確的檢測到代碼中所有的內存泄露,需要把代碼中的#define……拷貝到所有使用new的文件中。不可能每個文件都拷貝這麼多代碼,所以我們可以將他提取出來,放在一個文件中,比如我是放在 KDetectMemoryLeak.h中,該文件內容如下:

#pragma once

#ifdef _DEBUG

#define DEBUG_CLIENTBLOCK    new( _CLIENT_BLOCK, __FILE__, __LINE__)

#else

#define DEBUG_CLIENTBLOCK

#endif

#define _CRTDBG_MAP_ALLOC

#include <stdlib.h>

#include <crtdbg.h>

#ifdef _DEBUG

#define new DEBUG_CLIENTBLOCK

#endif

然後將KDetectMemoryLeak.h包含在項目的通用文件中,例如用VS建的項目就將其包含在stdafx.h中。或者我自己建的一個Common.h文件中,該文件包含一些通用的,基本所有文件都會用到的代碼東東。
好了,到現在,檢測內存泄露總算完成了,而且他還能定位到到底是代碼中哪個文件,哪行出現了內存泄露。下一篇文章將會講些實際遇到的一些問題,例如只知道有內存泄露,但是不知道到底內存泄露的具體位置,如何利用內存斷點等技術來定位內存泄露的位置啊,最後會從代碼的角度講下,怎麼樣才能避免內存泄露吧。

注:文章轉載自天極網
作者:出處:計算機教學網責任編輯:方舟
 

下面是我自己測試的時候出現的問題

 

#include <vector>

#include <string>

#include <iostream>

#ifdef _DEBUG

#define DEBUG_CLIENTBLOCK    new( _CLIENT_BLOCK, __FILE__, __LINE__)

#else

#define DEBUG_CLIENTBLOCK

#endif

#define _CRTDBG_MAP_ALLOC

#include <crtdbg.h>

#ifdef _DEBUG

#define new DEBUG_CLIENTBLOCK

#endif

using namespace std;

void main()

{

int * p = new int;

int i = 5;

    p = &i;

delete p;

_CrtDumpMemoryLeaks();

}

 

代碼編譯通過,運行的時候直接跳到如下的錯誤處:

在網上查資料,發現自己出現了一個嚴重的錯誤,自己對p進行new時候的地址已經在 p=& i; 的時候給修改了,所以當delete p;的時候p已經不是自己申請的那段內存了。貼出下面代碼,看的更清楚一些:

#include   <iostream.h>   
  #include   <string.h>   
  int   main()   
  {   
      char   *p   =   new   char[5];   
      if(p   !=   NULL)   
      {   
          strcpy(p,   "abcd");   
          cout   <<   p   <<   endl;   
          delete   []   p;   
      }   
      else   
      {   
          cout   <<   "failed!"   <<   endl;   
      }   
      return   0;   
  }  

這時候的p就可以進行delete,原因是Strcpy函數(原型)在操作的時候,並沒有改變p的地址。

明白之後將最初自己的那段代碼作如下調整:

void main()

{

int * p = new int;

int i = 5;

    //p = &i;

    * p = i ;

delete p;

_CrtDumpMemoryLeaks();

}

 

程序運行順利通過。這時候的代碼還不安全,重寫如下:

void main()

{

 

int * p = new int;

    assert(NULL != p);

    if(NULL == p)

    {

        exit(1);

    }

int i = 5;

    //p = &i;

    * p = i;

delete p;

    p = NULL;

}

 

貼出如下的代碼:

#include <vector>

#include <string>

#include <iostream>

#include<cassert>

#ifdef _DEBUG

#define DEBUG_CLIENTBLOCK    new( _CLIENT_BLOCK, __FILE__, __LINE__)

#else

#define DEBUG_CLIENTBLOCK

#endif

#define _CRTDBG_MAP_ALLOC

#include <crtdbg.h>

#ifdef _DEBUG

#define new DEBUG_CLIENTBLOCK

#endif

using namespace std;

 

 

 

class TestMemory

{

public:

    TestMemory();

    ~TestMemory();

public:

    void SetValue(int tempData);

private:

    int m_iTestData;

};

 

TestMemory::TestMemory()

{

 

}

TestMemory::~TestMemory()

{

 

}

void TestMemory::SetValue(int tempData)

{

    m_iTestData  = tempData;

}

 

void main()

{

    vector<TestMemory *> pTestMemory;

    TestMemory *test1 = new TestMemory();

    test1->SetValue(1);

 

    TestMemory *test2 = new TestMemory();

    test2->SetValue(2);

 

    TestMemory *test3 = new TestMemory();

    test3->SetValue(3);

 

    TestMemory *test4 = new TestMemory();

    test4->SetValue(4);

 

    pTestMemory.push_back(test1);

    pTestMemory.push_back(test2);

    pTestMemory.push_back(test3);

    pTestMemory.push_back(test4);

 

    TestMemory  * test = new  TestMemory();

    * test = * pTestMemory[2];

 

 

    //for(int i =0;i<4;i++)

    //{

    //  delete pTestMemory[i];

    //}

 

    //delete test;

 

    _CrtDumpMemoryLeaks();

 

}

 

不進行delete 這時候output會有下面的調試內容出現:

雙擊client block at所在行,就可以直接定位到內存泄露的地方,去掉代碼中屏蔽的地方程序正常。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章