[PyKDE] Memory leak with QDialog

Phil Thompson phil at riverbankcomputing.co.uk
Mon Feb 27 10:20:57 GMT 2006


On Monday 27 February 2006 9:43 am, Aurélien Gâteau wrote:
> Hello,
>
> (Maybe this is not new to you, sorry if it's the case.)
>
> I have been monitoring a big PyQt application lately, trying to reduce
> memory usage. I noticed that QDialog causes memory leaks.
>
> Using C++, one can use a dialog like this:
>   void function() {
>     MyDialog dlg(this);
>     dlg.exec();
>   }
>
> The Python equivalent is:
>
>   def function(self):
>     dlg=MyDialog(self)
>     dlg.exec_loop()
>
> Except that this is not totally equivalent:
> - In the C++ version, dlg is a local variable: so it gets deleted when we
> get out of function().
> - In the Python version, dlg is owned by self: when we get out of
> function(), it is still referenced and won't get garbage collected. A
> workaround to that problem is to reparent the dialog, like this:
>
>   def function(self):
>     dlg=MyDialog(self)
>     dlg.exec_loop()
>     dlg.reparent(None, QPoint(0,0))
>
> The attached Python example demonstrates this.
>
> If there is a better solution, let me know.

You don't say what versions you are using. I think this was fixed in v3.15 - 
at least I don't see any leak in current snapshots.

Phil




More information about the PyQt mailing list