2011-07-26 18 views

Respuesta

19

Después de muchos intentos por fin encontré un método para detectar las fugas de memoria de un proyecto Qt en Windows:

1) En primer lugar, no se puede hacer directamente en Qt Creator por lo que necesita para crear un Visual C++ proyecto para hacer la detección de fuga de memoria Afortunadamente, qmake lo hace fácil. Abra la herramienta de línea de comandos de Qt SDK y ejecute:

qmake -spec win32-msvc2008 -tp vc 

Esto convertirá su proyecto a .vcproj.

2) Abrir este proyecto y añadir el código necesario para la detección de fugas de memoria:

Para su main.cpp archivo:

// 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) Con esto, el proyecto debe ser ahora capaz para detectar fugas de memoria. Tenga en cuenta que _MSC_VER define para que este código solo se ejecute cuando lo ejecute desde Visual C++ (no desde Qt Creator). Significa que aún puede hacer el desarrollo con Qt Creator y simplemente volver a ejecutar el paso 1 siempre que necesite verificar si hay pérdidas de memoria.

4) a romperse en una asignación de memoria determinada, utilice _CrtSetBreakAlloc() Más información memoria de detección de fugas en el sitio web de Microsoft: http://msdn.microsoft.com/en-us/library/e5ewb1h3%28v=vs.80%29.aspx

5

Nuevo 2017 solución

cita de @this.lau_

En primer lugar, no se puede hacer directamente en Qt Creator, por lo que necesita crear un proyecto de Visual C++ para realizar la detección de fuga de memoria. Afortunadamente, qmake lo hace fácil.

1) Abra la herramienta de línea de comandos Qt SDK y ejecute:

qmake -spec win32-msvc2015 -tp vc

2) Instalar Visual Leak Detector for Visual C++

3) Abrir un .vcxproj que se creó con el paso 1

4) Incluir en su main.cpp

#include <vld.h>

5) Lanzamiento DebugView v4.81

6) Que ejecute su proyecto ctrl + F5