2011-07-26 40 views

回答

19

多次嘗試後,我終於找到了檢測Windows上的Qt工程的內存泄漏的方法:

1)首先,它不能直接在Qt Creator的,所以你需要創建一個Visual C++項目完成做內存泄漏檢測。值得慶幸的是,qmake使這一切變得簡單。打開了Qt SDK命令行工具和運行:

qmake -spec win32-msvc2008 -tp vc 

這將您的項目轉換成的.vcproj。

2)打開這個項目,並添加必要的代碼的內存泄漏檢測:

要將的main.cpp文件:

// Necessary includes and defines for memory leak detection: 
#ifdef _MSC_VER 
#define _CRTDBG_MAP_ALLOC 
#include <crtdbg.h> 
#endif // _MSC_VER 


#if defined(_MSC_VER) 

// Code to display the memory leak report 
// We use a custom report hook to filter out Qt's own memory leaks 
// Credit to Andreas Schmidts - http://www.schmidt-web-berlin.de/winfig/blog/?p=154 

_CRT_REPORT_HOOK prevHook; 

int customReportHook(int /* reportType */, char* message, int* /* returnValue */) { 
    // This function is called several times for each memory leak. 
    // Each time a part of the error message is supplied. 
    // This holds number of subsequent detail messages after 
    // a leak was reported 
    const int numFollowupDebugMsgParts = 2; 
    static bool ignoreMessage = false; 
    static int debugMsgPartsCount = 0; 

    // check if the memory leak reporting starts 
    if ((strncmp(message,"Detected memory leaks!\n", 10) == 0) 
    || ignoreMessage) 
    { 
    // check if the memory leak reporting ends 
    if (strncmp(message,"Object dump complete.\n", 10) == 0) 
    { 
     _CrtSetReportHook(prevHook); 
     ignoreMessage = false; 
    } else 
     ignoreMessage = true; 

    // something from our own code? 
    if(strstr(message, ".cpp") == NULL) 
    { 
     if(debugMsgPartsCount++ < numFollowupDebugMsgParts) 
     // give it back to _CrtDbgReport() to be printed to the console 
     return FALSE; 
     else 
     return TRUE; // ignore it 
    } else 
    { 
     debugMsgPartsCount = 0; 
     // give it back to _CrtDbgReport() to be printed to the console 
     return FALSE; 
    } 
    } else 
    // give it back to _CrtDbgReport() to be printed to the console 
    return FALSE; 
} 

#endif 



int main(int argc, char *argv[]) { 
    #if defined(_MSC_VER) 
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); 
    prevHook = _CrtSetReportHook(customReportHook); 
    // _CrtSetBreakAlloc(157); // Use this line to break at the nth memory allocation 
    #endif 

    QApplication* app = new QApplication(argc, argv); 
    int appError = app->exec(); 
    delete app; 

    #if defined(_MSC_VER) 
    // Once the app has finished running and has been deleted, 
    // we run this command to view the memory leaks: 
    _CrtDumpMemoryLeaks(); 
    #endif 

    return appError; 
} 

3)有了這個,你的項目現在應該能夠檢測內存泄漏。請注意0​​定義,以便此代碼僅在從Visual C++(而不是從Qt Creator)運行時執行。這意味着您仍然可以使用Qt Creator進行開發,並且只要需要檢查內存泄漏時重新運行第1步。

4)爲了打破在特定的內存分配,使用_CrtSetBreakAlloc()更多信息內存泄漏檢測上微軟的網站:http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=vs.80%29.aspx

5

新的2017年解決方案

報價由@this.lau_

首先,它不能直接在Qt Creator中完成,因此您需要創建一個Visual C++項目 來執行內存泄漏檢測。謝天謝地, qmake使這一切變得簡單。

1)打開Qt的SDK命令行工具和運行:

qmake -spec win32-msvc2015 -tp vc

2)安裝Visual Leak Detector for Visual C++

3)打開,將其與步驟1

創建.vcxproj

4)納入您的main.cpp

#include <vld.h>

5)啓動DebugView v4.81

6)比運行項目ctrl + F5