Qt在VS中內存溢出檢測



方法一:
參考鏈接1: http://blog.csdn.net/dizuo/article/details/6030676
參考鏈接1參考鏈接2說的是一個東西,不使用任何工具,自己寫程序判斷溢出位置。

方法二:
參考鏈接3參考鏈接4是用VLD(Visual Leak Detector)來檢測內存溢出,當然更加專業。

問題描述:
今天在QT_VP中簡單地添加了內存泄露檢測語句:

_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

結果出來一大堆的內存泄露,着實嚇了一跳,跟蹤了一天,逐段派出,最後還是感覺沒問題(還是比較自信代碼質量的O(∩_∩)O哈哈~)。。。最後上網一查,在vs中,先是進行內存泄露報告,然後才卸載Qt的dll,釋放申請的資源。所以纔會出現這種情況。

系統環境:
windows7 + VS2008;

方法一:
操作方法與參考鏈接1做法一致,如下:
標誌內存檢測報告:setdebugnew.h(注意,只有在cpp中添加setdebugnew.h,而且有new操作的那些函數或者類被調用,纔會進行內存泄露跟蹤)
#ifndef SET_DEBUG_NEW_H
#define SET_DEBUG_NEW_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

#endif // end SET_DEBUG_NEW_H

在需要檢測的cpp文件中包含setdebugnew.h頭文件
test_memoryleak.cpp
#include "test_memoryleak.h"
#include <QtGui>

#include "setdebugnew.h"

test_memoryLeak::test_memoryLeak(QWidget *parent, Qt::WFlags flags)
      : QWidget(parent, flags)
{
      btn1 = new QPushButton("test1", this);
      btn2 = new QPushButton("close", this);

      QWidget *memLeak_test = new QWidget;

      connect(btn2, SIGNAL(clicked()), this, SLOT(close()));

      QHBoxLayout *layout = new QHBoxLayout;

      layout->addWidget(btn1);
      layout->addWidget(btn2);

      setLayout(layout);
}

test_memoryLeak::~test_memoryLeak()
{

}

main.cpp(在主函數入口處設置_CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );)
#include "test_memoryleak.h"
#include <QtGui/QApplication>

#include "setdebugnew.h"

int main(int argc, char *argv[])
{
#ifdef _DEBUG 
      _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

      int *i = new int;

      QApplication a(argc, argv);
      test_memoryLeak w;
      w.show();
      return a.exec();
}

啓動調試,在程序運行結束後,查看“輸出”項中的內容
{1220} normal block at 0x016605F8, 552 bytes long.
 Data: <          f     > 83 00 00 00 83 00 00 00 08 06 66 01 CD CD CD CD
{1215} normal block at 0x01660398, 292 bytes long.
 Data: <   eH f      x=g> 94 85 A2 65 48 03 66 01 00 00 00 00 C4 78 3D 67
.\test_memoryleak.cpp(12) : {1214} client block at 0x01660348, subtype 0, 20 bytes long.
.
.
.
{289} normal block at 0x01388960, 56 bytes long.
 Data: <      8         > 03 00 00 00 88 8A 38 01 00 CD CD CD 00 00 00 00
.\main.cpp(12) : {285} client block at 0x013872B0, subtype 0, 4 bytes long.
 Data: <    > CD CD CD CD
Object dump complete.

黑體加粗部分的纔是程序真正溢出的地方。

方法二:
使用VLD檢測內存溢出,VLD下載地址: http://vld.codeplex.com/releases/view/82311(2.2.3版只支持VS2008/2010 )
安裝過程會彈出如下對話框,

新版解決了path問題,所以我們無需做過多的設置。

安裝完成後,還需要對VS的編譯路徑進行設置,
在“工具”->“選項”->”項目和解決方案“->"VC++目錄" 添加VLD的”包含文件“路徑和”庫文件“路徑,如下如所示:

注意:參考鏈接4提到了,在win7中需要拷貝dbghelp.dll和vdl_x86.dll到system32目錄,而我在配置的時候並不需要這麼做。

在main.cpp中包含vld.h頭文件即可,如下:
#include "test_memoryleak.h"
#include <QtGui/QApplication>

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

#include "setdebugnew.h"


int main(int argc, char *argv[])
{
#ifdef _DEBUG 
      _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
#endif

      int *i = new int;

      QApplication a(argc, argv);
      test_memoryLeak w;
      w.show();
      return a.exec();
}

啓動調試,在程序運行結束後,查看“輸出”項中的內容
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x007F72B0: 4 bytes ----------
  Call Stack:
    c:\users\ajaxhe\desktop\program\qt\practice\test_memoryleak\test_memoryleak\main.cpp (17): test_memoryLeak.exe!main + 0x10 bytes
    C:\Qt\4.7.4\src\winmain\qtmain_win.cpp (131): test_memoryLeak.exe!WinMain + 0x12 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (578): test_memoryLeak.exe!__tmainCRTStartup + 0x35 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): test_memoryLeak.exe!WinMainCRTStartup
    0x76B9ED6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77D1377B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes
    0x77D1374E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes
  Data:
    CD CD CD CD                                                  ........ ........


---------- Block 4 at 0x00CA0348: 20 bytes ----------
  Call Stack:
    c:\users\ajaxhe\desktop\program\qt\practice\test_memoryleak\test_memoryleak\test_memoryleak.cpp (12): test_memoryLeak.exe!test_memoryLeak::test_memoryLeak + 0x10 bytes
    c:\users\ajaxhe\desktop\program\qt\practice\test_memoryleak\test_memoryleak\main.cpp (20): test_memoryLeak.exe!main + 0x17 bytes
    C:\Qt\4.7.4\src\winmain\qtmain_win.cpp (131): test_memoryLeak.exe!WinMain + 0x12 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (578): test_memoryLeak.exe!__tmainCRTStartup + 0x35 bytes
    f:\dd\vctools\crt_bld\self_x86\crt\src\crtexe.c (403): test_memoryLeak.exe!WinMainCRTStartup
    0x76B9ED6C (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77D1377B (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xEF bytes
    0x77D1374E (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0xC2 bytes
  Data:
    04 7C FB 00    98 03 CA 00    E0 7B FB 00    00 00 CD CD     .|...... .{......
    50 04 CA 00                                                  P....... ........


Visual Leak Detector detected 2 memory leaks (96 bytes).
Largest number used: 260 bytes.
Total allocations: 260 bytes.
Visual Leak Detector is now exiting.

溢出位置與預期一致!


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