[PyQt] Again, problems with garbage collection

Phil Thompson phil at riverbankcomputing.com
Thu Mar 18 17:19:58 GMT 2010


On Thu, 18 Mar 2010 15:48:25 +0100, Martin Teichmann
<lkb.teichmann at gmail.com> wrote:
> Hi List,
> 
> I just upgraded to PyQt4 4.7.2 (with Python 2.6.4 on Windows XP)
> and the following little script crashes with "unterlying C/C++ object
> has been deleted"
> 
> I guess that's a bug.

Why? It works as I would expect.

> ------------------- snip ------------------------------
> from PyQt4.QtCore import QObject, SIGNAL, QCoreApplication
> 
> # following two lines not important
> from sys import argv
> app = QCoreApplication(argv)
> 
> class A(QObject):
>     def __init__(self, p=None):
>         QObject.__init__(self, p)
> 
>     def g(self):
>         self.emit(SIGNAL("sig"))
> 
>     def f(self):
>         print "hello"
> 
>     def kill(self):
>         self.setParent(None)
> 
> b = A()
> a = A(b)

So a's parent is b.

> b.connect(a, SIGNAL("sig"), b.f)
> a.g()
> b.kill()

This has no effect as b doesn't have a parent.

> b = None

This garbage collects the original b. As it didn't have a parent it also
destroys the C++ instance. Because b's C++ instance owns a's it also
destroys a's C++ instance. Because a is still bound to it, the Python
object isn't garbage collected.

> a.g()

self.emit needs a's C++ instance which has been destroyed - so it raises
the exception.

> ------------------------------- snip -------------------------

Phil


More information about the PyQt mailing list