[PyQt] Signals and "RuntimeError: wrapped C/C++ object of type XXX has been deleted"

Phil Thompson phil at riverbankcomputing.com
Wed Nov 20 19:14:06 GMT 2013


On Wed, 20 Nov 2013 19:57:07 +0100, Jeremy Sanders
<jeremy at jeremysanders.net> wrote:
> I'm getting problems in my app where a Python QWidget object receives 
> signals after the C/C++ widget has been deleted. This gives the usual 
> frustrating error "RuntimeError: wrapped C/C++ object of type XXX has 
> been deleted".
> 
> I'm emitting old-style Python signals which look like this
>          self.emit( qt4.SIGNAL("sigModified"), ismodified )
> ...
>          self.connect(self.document, qt4.SIGNAL("sigModified"),
>                       self.slotDocModified)
> 
> What I'm doing is listening for changes in a document in a widget class,

> which shows a document status.
> 
> I would have thought that when a C++ object (the widget) is deleted, the

> signal should become disconnected. This doesn't seem to be happening - 
> the connection remains after the C++ object has gone.
> 
> This is with
> Python version: 2.7.1/2.7.4 (seen on Linux and Windows)
> Qt version: 4.8.5
> PyQt version: 4.10.3 / 4.10
> SIP version: 4.15.3 / 4.14.5
> 
> Is this a bug? I've managed to fix a couple of these where it's obvious 
> where the widget is deleted, so I disconnect the signal first. In other 
> places I can't see an obvious way to do this - where can I clear up 
> signals of random widgets? This seems like a hack and I would have 
> thought that signals should be deleted before the C++ object is deleted.

It's possible there is some sort of race condition, but difficult to tell
without an example.

The use of Python signals and slots can create proxies at both end of the
connection. Moving to new-style signals (and maybe decorated slots) should
simplify things.

Phil


More information about the PyQt mailing list