[PyKDE] Widget Counting and Garbage Collecting

bdw bdw_box at yahoo.co.uk
Fri Jan 21 19:42:14 GMT 2005


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?

Can I rely on the -widgetcount flag when debugging my PyQt apps, or am
I going about this in completely the wrong way?

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!?











=====
bdw


	
	
		
___________________________________________________________ 
ALL-NEW Yahoo! Messenger - all new features - even more fun! http://uk.messenger.yahoo.com




More information about the PyQt mailing list