[PyQt] Garbage collection, signals to partial functions

Lukas Hetzenecker LuHe at gmx.at
Wed Oct 28 22:13:38 GMT 2009


This seems to work for me too.

lukas at laptop ~  $  python gb.py
None <__main__.A object at 0x7f9baff6ef30> <functools.partial object at 
0x7f9baa5f3ba8> <bound method A.f of <__main__.A object at 0x7f9baff6ef30>>
None <__main__.A object at 0x7f9baff6ef30> <functools.partial object at 
0x7f9baa5f3ba8> <bound method A.f of <__main__.A object at 0x7f9baff6ef30>>
2 []

python-2.6-9.fc11.x86_64 and PyQt4-4.5.4-1.fc11.x86_64 under Linux

Am Mittwoch 28 Oktober 2009 18:35:13 schrieb Martin Teichmann:
> 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())
> 
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> 


More information about the PyQt mailing list