[Qt-interest] How to detect memory leaks

Constantin Makshin cmakshin at gmail.com
Mon Jul 12 13:25:23 CEST 2010


When developing Qt applications, I usually write my "main.cpp" this way:

...
int main (int argc, char** argv)
{
    QApplication app(argc, argv);
    ...
    return app.exec();
}

#if defined(_MSC_VER) && !defined(Q_OS_WINCE)

#include <stdlib.h>
#include <windows.h>
#include <crtdbg.h>

int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)
{
    // Enable automatic memory leak detection
    _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
    return main(__argc, __argv);
}

#endif

In this case you don't need to explicitly call _CrtMem*() functions, your main() function remains free of platform-specific memory leak detection routines and you don't have to use the "qtmain" library when compiling with MSVC.
In fact, the most significant difference of my WinMain() and WinMain() from "qtmain.lib" is the lack of call to "qWinMain()" function which doesn't seem to be [very] useful anyway. :)

On Monday 12 July 2010 13:07:34 Johnson Ma wrote:
> I think you can use _CrtMemCheckpoint, _CrtMemDifference to detect memory
> leak on Windows.
> 
> For Linux, since there is no similar API to do the heap check, you can use
> Valgrind (VALGRIND_COUNT_LEAKS)
> 
> Regards
> 
> Johnson
> 
> 
> 
> 
> On Mon, Jul 12, 2010 at 4:46 PM, Santhosh Y <santhosh at softjin.com> wrote:
> 
> >  Hi,
> >
> > How to see memory leaks in my Qt application. Would like to know the amount
> > of memory leak in my application.
> >
> > I am working on Windows OS,  using
> >
> > 1. Microsoft Visual Studio 2008
> >     Version 9.0.21022.8 RTM
> >     Microsoft .NET Framework
> >     Version 3.5 SP1
> > 2. Qt 4.5.2 version
> >
> >  1. Any idea of how to run purify for a Qt application.
> > 2. Any flags to be added to the project file / Visual studio for detecting
> > memory leaks?
> >
> > Thanks in advance.
> >
> > Regards,
> > Santhosh



More information about the Qt-interest-old mailing list