[Qt-interest] Question about detecting memory leaks!

Jack Mack jackmack2000de at yahoo.de
Fri Dec 17 11:02:48 CET 2010


Hello Marcelo,

thanks for your information. I think for the first time it's enough to know on what I have to watch to avoid memory leaks.

Thanks a lot and if I have more questions about memory leaks I contact you.

Regards
jackmack

--- Marcelo Magno T. Sales <mmtsales at gmail.com> schrieb am Do, 16.12.2010:

Von: Marcelo Magno T. Sales <mmtsales at gmail.com>
Betreff: Re: [Qt-interest] Question about detecting memory leaks!
An: qt-interest at trolltech.com
Datum: Donnerstag, 16. Dezember, 2010 22:48 Uhr

Hello, Jack

In most Qt examples, the main form of the application (usually a QDialog 
or a QMainWindow) is created in stack before calling 
QApplication::exec() in main(). The dinamically created widgets are made 
children of the main form. In this case, they do not need to be 
explicitly deleted, because the main form will be destroyed when main() 
ends and will then delete all of its children, which were created 
dinamically. However, if you dinamically create a top level form (not 
child of any other Qt widget) and its children and forget do explicitly 
delete the form before you lose the pointer to it (when it runs out of 
scope or if you change the address to which it was pointing), the form 
and all of its children will remain in memory and you will not be able 
to access them anymore. That memory will be lost until your application 
finishes (in other words, you will have a memory leak).
Maybe if you can show an example where you think there may be a problem, 
we could comment on it.

[]'s
Marcelo

Em quinta-feira 16 dezembro 2010, Jack Mack escreveu:
> Hello Marcelo,
> 
> What you wrote is confusing me.
> When I look to Qts examples there are always widgets are dynamically
> created and NEVER explicitly deleted.
> 
> I thought Trolltechs developers know what they do!
> 
> Regards
> Jack
> 
> --- Marcelo Magno T. Sales <mmtsales at gmail.com> schrieb am Mi,
> 15.12.2010:
> 
> Von: Marcelo Magno T. Sales <mmtsales at gmail.com>
> Betreff: Re: [Qt-interest] Question about detecting memory leaks!
> An: qt-interest at trolltech.com
> Datum: Mittwoch, 15. Dezember, 2010 18:42 Uhr
> 
> You have allocated memory for an int with new, but did not freed it
> with delete before terminating your program.  A leak is to be
> expected. Your assumption about all instances of classes derived
> from QObject being deleted automatically is also faulty.
> QObjects are deleted automatically only if their parents are
> destroyed. If you create the parent in heap and do not delete it
> manually, you will have leaks also. If you create the parent in the
> stack, then it will be destroyed when it runs out of scope and will
> automatically delete all of its children, which will then delete
> their children and so on. In this case there will be no leaks.
> 
> []'s
> Marcelo
> 
> Em quarta-feira 15 dezembro 2010, Jack Mack escreveu:
> > Hi all,
> > 
> > I have a question about detecting memory leaks in my qt based
> > application.
> > 
> > I'm using Qt 4.7.0 with VS2008.
> > 
> > Maybe this is a simple question and I get a fast answer.
> >
> > This is my snipped out code of my qt based application:
> >     int* a = new int;
> >     *a = 1;
> >
> > When I start the app in debug-mode within VS2008 and finish the app
> > correctly by clicking the windows system menu then the output
> > window of VS2008 shows only some text like:
> > 
> > ...
> > Thread 'Win32 Thread' (0xf68) finished with code 0 (0x0).
> > BTMMHOOK 15.12.2010 12:19:31 Thread<0460> Hook DLL unloaded
> > Thread 'Win32 Thread' (0x132c) finished with code 0 (0x0).
> > Program "[4052] LineView.exe: Systemown" finished with code 0
> > (0x0).
> > 
> > When I use the snipped code above in a simple MFC application and
> > run it in debug-mode and close it then the following text is
> > showed by output window of VS2008:
> > 
> > ...
> > BTMMHOOK 15.12.2010 12:21:16 Thread<0E14> Hook DLL unloaded
> > Detected memory leaks!
> > Dumping objects ->
> > e:\entwicklung\simplemfc\simplemfc\simplemfc.cpp(77) : {565} normal
> > block at 0x0034B790, 4 bytes long. Data: <    > 01 00 00 00
> > Object dump complete.
> > Program "[2160] SimpleMFC.exe: Systemown" finished with code 0
> > (0x0).
> > 
> > I think that's strange! The compiler warning options are the same.
> > Yes, I know that all classed inherited by QObject are deleted
> > automatically and "delete" is not neccessary. But the snipped code
> > above causes definitly a memory leak.
> > 
> > Can someone explain that to me?
> > 
> > Thanks a lot,
> > jack
> 
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-interest
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101217/5ccccc09/attachment.html 


More information about the Qt-interest-old mailing list