How to detect memory leaks in QtCreator on Windows?

Viewed 24165

How can I detect memory leaks in QtCreator on Windows? On the doc, they recommend Memcheck but it only works on Mac and Linux. Any suggestion for Windows?

6 Answers

Here's an even more recent answer. Qt Creator 4.7.1 now supports heob, which is a leak detector too. You can down it for Windows from here: "heob download | SourceForge.net". Extract it somewhere, get a recent version of Qt Creator, and go to Analyze | heob. Direct it to yer .exe, choose yer options, click the little disk icon to save yer opts, and click OK to run yer proggie. It gives u a nice little memcheck window that seems to give you stack dumps at the time the objects were allocated, which u can unfold and see the offending objects; when you use the Detect Leak Types option. You can even navigate to the source line where the new occurred by clicking the link.

JBES supplies the following info:

Optionally download the dwarfstack DLLs for proper stacktrace resolution if you compile with MinGW from dwarfstack DLLs download | github.com and put them in the same folder as the heob executables.

New solution for 2019...

  1. Don't bother making a Visual Studio project.

  2. Install Visual Leak Detector for Visual C++

  3. Include #include <vld.h> in at least one source file.

  4. Add this to your .pro file...

INCLUDEPATH += "C:/Program Files (x86)/Visual Leak Detector/include/" LIBS += -L"C:/Program Files (x86)/Visual Leak Detector/lib/Win64"

  1. On the Project tab, under Build & Run / Run / Run Environment, to the PATH add:

C:\Program Files (x86)\Visual Leak Detector\bin\Win64

If you intend to run the program outside of Qt, you may need to copy dll files (dbghelp and vld_x64) to your build folder, where your program will run. The dlls files are at C:\Program Files (x86)\Visual Leak Detector\bin and you may need to copy vcruntime140.dll or the version of VS that built Visual Leak Detector.

  1. Run qmake, build and develop in Qt-Creator as normal, and the leaks will appear in the Application Output window.

From some version, Deleaker can work as a plugin for Qt Creator. After installation, you can open Deleaker window from the Qt Creator main menu. Once debugging started, Deleaker is monitoring allocations and deallocations. At any moment you can take a snapshot to get list of currently allocated resources (not memory only, but also GDI and others). A video tutorial is available: https://www.youtube.com/watch?v=Fomfpeuc-0o

PS I am from Softanics that is working on Deleaker.

when using visual studio, it is important to note that if your mainwindow is created on the stack like so :

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    int result = a.exec();
    _CrtDumpMemoryLeaks();
    return result;
}

Many false positive will appear if you dinamically allocate memory in you Qt objects. To avoid it, one can to modify the code to create the window object on the heap and execute the function _CrtDumpMemoryLeaks() after deletion of the mainwindow instance:

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow * = new w;
    w->show();
    int result = a.exec();
    delete w;
    _CrtDumpMemoryLeaks();
    return result;
}
Related