[PyKDE] Memory leak with QDialog

Aurélien Gâteau agateau at dental-on-line.fr
Mon Feb 27 09:43:57 GMT 2006


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.
-- 
Aurélien Gâteau - agateau at dental-on-line.fr
Dental-on-line
23 rue du Départ
75014 PARIS - FRANCE
-------------- next part --------------
A non-text attachment was scrubbed...
Name: qdialogleak.py
Type: text/x-python
Size: 1604 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20060227/d9e28b6c/qdialogleak.py


More information about the PyQt mailing list