C/C++程序內存泄漏檢測





C++內存泄露的檢測(一)


Visual Studio 調試器和 C 運行時 (CRT) 庫爲我們提供了檢測和識別內存泄漏的有效方法。主要使用函數:_CrtDumpMemoryLeaks();

二 實例
#define _CRTDBG_MAP_ALLOC   //輸出更詳細的report
#include <stdlib.h>
#include <crtdbg.h>
//以上的內容必須放在其他include的前面

#include <vector>

class MyClass
{
private:
    int *p;
public:
    MyClass()
    {
        if(p != NULL)
        {
            p = new int(0);
        }

    }

    ~MyClass()
    {
        if(p != NULL)
        {
            delete p;
            p = NULL;
        }

    }

}
;

int _tmain(int argc, _TCHAR* argv[])
{
    int *i = NULL; // better for read
    i = new int(0);    
    int *&y = i; // pointer's reference

    MyClass *pMyClass = new MyClass();

    std::vector<MyClass*> myClasses;
    myClasses.push_back(new MyClass());
    myClasses.push_back(new MyClass());

    _CrtDumpMemoryLeaks();
    return 0;
}

三說明
1)只對debug模有用,可以在程序運行後在vs的ide的output的最後看到泄露的檢測結果。
2)可以檢測系統類型,自定義類型和stl 容器。
3)#define _CRTDBG_MAP_ALLOC   //包含該宏定義輸出更詳細的report
      #include <stdlib.h>
      #include <crtdbg.h>
      //以上的內容必須放在其他include的前面,否則可能使上面定義的宏失效。
4)如果程序有統一的退出口,則在退出時調用_CrtDumpMemoryLeaks();
5)如果程序有多個出口,則可以在程序開始處包含下面的調用:_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );這條語句無論程序在什麼地方退出都會自動調用 _CrtDumpMemoryLeaks。

四 更多(更多的API和demo的下載)
http://msdn2.microsoft.com/zh-cn/library/fxszt639(VS.80).aspx

五 其他同類文章
http://www.cppblog.com/zhouhuishine/archive/2008/01/22/41609.html


C++內存泄露的檢測(三)



一 簡單的對內存的分配和釋放跟蹤,並將結果輸出到console,它也是一般C++內存泄露的檢測原理,來自C++編程思想:
(比較簡單,大家都可以看的明白的哦)實現如下:

MemCheck.h
//: C02:MemCheck.h
#ifndef MEMCHECK_H
#define MEMCHECK_H
#include <cstddef>  // for size_t

// Hijack the new operator (both scalar and array versions)
voidoperator new(std::size_t, const char*, long);
voidoperator new[](std::size_t, const char*, long);
#define new new (__FILE__, __LINE__)

extern bool traceFlag;
#define TRACE_ON() traceFlag = true
#define TRACE_OFF() traceFlag = false

extern bool activeFlag;
#define MEM_ON() activeFlag = true
#define MEM_OFF() activeFlag = false

#endif
///:~

MemCheck.cpp
//: C02:MemCheck.cpp {O}
#include <cstdio>
#include <cstdlib>
#include <cassert>
using namespace std;
#undef new

// Global flags set by macros in MemCheck.h
bool traceFlag = true;
bool activeFlag = false;

namespace {

// Memory map entry type
struct Info {
  void* ptr;
  const char* file;
  long line;
}
;

// Memory map data
const size_t MAXPTRS = 10000u;
Info memMap[MAXPTRS];
size_t nptrs = 0;

// Searches the map for an address
int findPtr(void* p) 
{
  for (int i = 0; i < nptrs; ++i)
    if (memMap[i].ptr == p)
      return i;
  return -1;
}


void delPtr(void* p)
{
  int pos = findPtr(p);
  assert(p >= 0);
  // Remove pointer from map
  for (size_t i = pos; i < nptrs-1; ++i)
    memMap[i] = memMap[i+1];
  --nptrs;
}


// Dummy type for static destructor
struct Sentinel 
{
  ~Sentinel()
  {
    if (nptrs > 0) 
    {
      printf("Leaked memory at:\n");
      for (size_t i = 0; i < nptrs; ++i)
        printf("\t%p (file: %s, line %ld)\n",
          memMap[i].ptr, memMap[i].file, memMap[i].line);
    }

    else
      printf("No user memory leaks!\n");
  }

}
;

// Static dummy object
Sentinel s;

}
 // End anonymous namespace

// Overload scalar new
voidoperator new(size_t siz, const char* file,
  long line) {
  void* p = malloc(siz);
  if (activeFlag)
  {
    if (nptrs == MAXPTRS)
    {
      printf("memory map too small (increase MAXPTRS)\n");
      exit(1);
    }

    memMap[nptrs].ptr = p;
    memMap[nptrs].file = file;
    memMap[nptrs].line = line;
    ++nptrs;
  }

  if (traceFlag) 
  {
    printf("Allocated %u bytes at address %p ", siz, p);
    printf("(file: %s, line: %ld)\n", file, line);
  }

  return p;
}


// Overload array new
voidoperator new[](size_t siz, const char* file,
  long line) {
  return operator new(siz, file, line);
}


// Override scalar delete
void operator delete(void* p) 
{
  if (findPtr(p) >= 0) 
  {
    free(p);
    assert(nptrs > 0);
    delPtr(p);
    if (traceFlag)
      printf("Deleted memory at address %p\n", p);
  }

  else if (!p && activeFlag)
    printf("Attempt to delete unknown pointer: %p\n", p);
}


// Override array delete
void operator delete[](void* p) {
  operator delete(p);
}
 ///:~


二 說明:
1)通過重載new和delete來實現
2)使用時需要在工程中加入MemCheck.h和MemCheck.cpp,在需要檢測的文件的前面include “MemCheck.h”,但是必須在所有的include的最後。
3)MEM_ON(),MEM_OFF()用來打開或關閉檢測
4)TRACE_ON(),和TRACE_OFF()用來打開或關閉檢測結果的輸出
5)可以檢測代碼中使用了流,標準容器,以及某個類的構造函數分配了空間

三 使用實例:
console 的project中加入下面的file:
// MemoryLeak3.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <vector>
#include <cstring>

#include "MemCheck.h"   // Must appear last!
using namespace std;

void Test()
{
    int *i = new int(0);
}


class MyClass
{
private:
    int *p;
public:
    MyClass()
    {
        if(p != NULL)
        {
            p = new int(0);
        }

    }

    ~MyClass()
    {
        if(p != NULL)
        {
            delete p;
            p = NULL;
        }

    }

}
;

void Test2()
{
    int *i = NULL; // better for read
        i = new int(0);    
        int *&y = i; // pointer's reference
        delete i;

        MyClass *pMyClass = new MyClass();

        std::vector<MyClass*> myClasses;
        myClasses.push_back(new MyClass());
        myClasses.push_back(new MyClass());

        std::vector<void*> myVector;
        myVector.push_back(new MyClass());
        myVector.push_back(new MyClass());
        delete (MyClass *)(myVector.at(0));
        delete myVector.at(1); // memory leak
}


class Foo 
{
  char* s;
public:
  Foo(const char*s ) 
  {
    this->s = new char[strlen(s) + 1];
    strcpy(this->s, s);
  }

  ~Foo() 
  {
    delete [] s;
  }

}
;
void Test3()
{
    cout << "hello\n";
    int* p = new int;
    delete p;
    int* q = new int[3];
    delete [] q;
    int* r;
    /*delete r;*/
    vector<int> v;
    v.push_back(1);
    Foo s("goodbye");
}

int main() 
{
    TRACE_OFF();
    MEM_ON();
    Test();
    Test2();
    Test3();
    MEM_OFF();
}
 ///:~

四 測試結果如下:




C++內存泄露的檢測(四)

一 使用各種工具,一般都是收費的,但是可以申請試用。

二 工具收集
1)BoundsChecker :(http://www.compuware.com/)(首選BoundsChecker)
      應該說是功能最強,使用只需要open需要測試的exe,然後start就可以了,可以通過檢測結果定位到源代碼中有內存泄露的代碼行。
2)Purifyplus (http://www.ibm.com
3)Memory Validator(http://www.softwareverify.com/index.html
       應該說是功能也比較強,使用只需要start application wizard的start exe就可以了,可以通過檢測結果定位到源代碼中有內存泄露的代碼行。
4)其實以上工具還可以進行其他的各種檢測,提高代碼的健壯性!

三 
    工具只能幫助我們更好的發現泄露,但是並不能解決所有的問題,比如說我們的項目非常的複雜或是使用了多個第三方的lib,這樣的話,有可能使用以上的工具就檢測不到。
     豐富的編程經驗和良好的編程習慣才能夠徹底的杜絕內存的泄露。

簡單步驟

1. 包含頭文件和定義:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
如果定義了_CRTDBG_MAP_ALLOC,打印出來的是文件名和行數等更加直觀的信息。


2. 方法一
在程序入口寫幾個語句:
 int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
 tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
 _CrtSetDbgFlag( tmpFlag );
程序退出時,如果發現有內存泄漏,會自動在DEBUG OUTPUT窗口和DebugView中輸出內存泄漏信息。


3. 方法二
在程序任何地方用以下語句:
_CrtDumpMemoryLeaks();
隨時檢測打印內存泄漏信息,如果有的話。不過此用法有個侷限,對於一些全局函數,如果初始化時申請了內存,到程序結束時候才釋放,此函數會一直把新申請的內存當作泄漏來對待。


4. 方法三
使用_CrtMemCheckpoint方法,在某段程序中統計泄漏信息。如下:
 _CrtMemState s1, s2, s3;
 _CrtMemCheckpoint( &s1 );
 
 // 程序段1:DO SOMETHING


 _CrtMemCheckpoint( &s2 );
 if ( _CrtMemDifference( &s3, &s1, &s2) ) 
  _CrtMemDumpStatistics( &s3 );


可以統計程序段1中是否發生了內存泄漏。
發佈了40 篇原創文章 · 獲贊 5 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章