[PyQt] Garbage collection, signals to partial functions

Martin Teichmann martin.teichmann at mbi-berlin.de
Wed Oct 28 15:53:51 GMT 2009


Hi List,

I just connected a signal from one object "a" to a slot which
is a partial function, calling a method in another object "b".

That works fine, just when I don't need the objects anymore,
nothing is garbage collected. It seems that the reference
counter on the partial object is increased when connected,
as documented, but not decreased once object "a"
disappears.

I tried it on windows, python 2.5.4 and PyQt4 4.6
and linux, Python 2.6.2, PyQt4 4.4, no difference.

The program following this mail illustrates the bug.

Greetings

Martin Teichmann

---------------- snip ------------------------
import gc
from PyQt4 import QtCore
from weakref import ref
from functools import partial
import sys

class B(object):
    pass

class A(QtCore.QObject):
    def f(self, x):
        print x

# make 2 objects of A

o = B()
o.a = A()
o.b = A()

# connect a signal to a partial function
f = o.b.f
p = partial(f, 3)
o.a.connect(o.a, QtCore.SIGNAL("a"), p)
wf = ref(f)
wp = ref(p)
f = None
p = None

# kill all references
wa = ref(o.a)
wb = ref(o.b)
o.a = None
o.b = None

# but they're still here! (except wa)
# (line prints None, and then three objects)
print wa(), wb(), wp(), wf()

# OK, let's garbage collect
gc.collect()

# still everything there
# (line prints None, and then three objects)
print wa(), wb(), wp(), wf()

# that's the bug: still one reference, but no referrer
# (the line prints 2 [], getrefcount always gives 1 to high)
print sys.getrefcount(wp()), gc.get_referrers(wp())


More information about the PyQt mailing list