[PyQt] Exception in signal handler causes reference leak?

Oleg Klimov omgtech at gmail.com
Mon Dec 10 20:08:48 GMT 2007


Hello list,

I'm trying to reload parts of program on the fly (whole application is
really heavy to reload each time). I've experienced problems with
objects lifespan if I make mistake in source code editing.

Problems are apparently related to exception handling in signal/slot
mechanism. Exceptions do work as expected (i.e. unwind stack up to Qt
internals and print a backtrace), but it leaves references to python
objects.


--------

import gc
from PyQt4.QtGui import *
from PyQt4.QtCore import *


the_app = QApplication([])
the_app.shutdown_counter = 5

class TestWidget(QWidget):
        def hello(self):
                the_app.shutdown_counter -= 1
                if the_app.shutdown_counter==0:
                        the_app.quit()
                return
                self.no_such_symbol

        def __del__(self):
                print "DEL"

w = TestWidget()
w.show()

timer_hello = QTimer()
timer_hello.connect(timer_hello, SIGNAL("timeout()"), w.hello)
timer_hello.start(200)

the_app.exec_()
del w
del timer_hello
gc.collect()

print "At this point, TestWidget was not collected"


----

Run it first time, ensure "DEL" appears before "At this point...". Now
comment out "return" in signal handler, and here it is -- gc cannot
collect all the objects.

Problem reproduces on Windows ("big GPL package") and Ubuntu 7.10.


  Oleg


More information about the PyQt mailing list