[PyKDE] Widget Counting and Garbage Collecting
Phil Thompson
phil at riverbankcomputing.co.uk
Sat Jan 22 00:35:56 GMT 2005
On Friday 21 January 2005 7:42 pm, bdw wrote:
> I have always run my PyQt apps with Qt's -widgetcount flag because it
> seemed to be good at picking up things like circular references in my
> code.
>
> While using QScintilla recently, though, I started seeing some
> inconsistency in Qt's reports - sometimes it would show one widget left
> undestroyed, sometimes not. I eventually tracked this down to
> QScintilla's built-in context menu. Qt only reports one widget left if
> the context menu is shown.
>
> Here is a minimal QScintilla app which also uses Python's garbage
> collecting module:
>
> import sys, gc
> from qt import *
> from qtext import QextScintilla
> if __name__ == "__main__":
> gc.set_debug(gc.DEBUG_LEAK|gc.DEBUG_STATS)
> a = QApplication(sys.argv)
> a.connect(a,SIGNAL('lastWindowClosed()'),a,SLOT('quit()'))
> q = QextScintilla(); a.setMainWidget(q); q.show()
> a.exec_loop()
> del q
> gc.collect(); del gc.garbage[:]
>
> If I run this, right-click the editor, then close it, I get the
> following output:
>
> [bdw at localhost test]$ python qs-min.py -widgetcount
> gc: collecting generation 2...
> gc: objects in each generation: 492 2762 0
> gc: done.
> gc: collecting generation 2...
> gc: objects in each generation: 0 0 3248
> gc: done.
> Widgets left: 1 Max widgets: 6
>
> This seems to show that Python collects all its garbage, but Qt does
> not destroy all its Widgets. Or is it that Python killed the stray
> Widget and Qt is reporting out-of-date information?
QScintilla wasn't deleting the popup when it itself was being destroyed -
fixed in the next snapshot.
> Can I rely on the -widgetcount flag when debugging my PyQt apps, or am
> I going about this in completely the wrong way?
From the above it looks like the count is reliable.
> As a final test, I tried running eric with the -widgetcount flag and
> got the following output:
>
> [bdw at localhost eric-3.5.1]$ python eric3/eric3.py -widgetcount
> Widgets left: 650 Max widgets: 654
>
> Now that can't be right - or can it!?
I think there is a difference between a widget and an application. An
individual widget shouldn't leak memory (as QScintilla was), but I wouldn't
expect an application to tidy up on exit in the same way. An application that
leaks memory during use is a different matter - but that's not what your
figures measure.
Phil
More information about the PyQt
mailing list