[PyQt] Slots being called on deleted QObjects

Phil Thompson phil at riverbankcomputing.com
Sun Apr 6 16:59:04 BST 2014

On 06-04-2014 4:08 pm, Jeremy Sanders wrote:
> I'm still getting numerous bug reports from users where slots are 
> called on
> deleted QObjects, giving a crash. As I previously reported, changing 
> these
> to new style signals and using "@pyqtSlot" on the slot appears to 
> prevent
> these bugs, but it's very hard to find every case.
> Looking through the source code of sip (siplib/qtlib.c), it says:
>         /*
>          * We used to check that any wrapped C++ object still existed 
> and
> just
>          * returning None if it didn't.  This caused inconsistent 
> behaviour
>          * when the slot was a method connected to its object's 
> destroyed()
>          * signal.
>          */
> I assume this is the code used to call slots on QObjects? Is this the 
> reason
> why I started getting these problems a year or two ago?

The change appeared in v4.13.3 released June 2012.

> If this is the problem, is it possible to make the code more 
> intelligent and
> check if the C++ object is there as long as the signal isn't 
> destroyed()?

That would be an incompatible change. Any slot can be called from any 
signal. The problem only arises if you try to do something that requires 
the underlying C++ QObject. The comment mentions destroyed() only 
because that was the context in which the bug was reported (and it is 
guaranteed to happen with that particular signal) but it can apply to 
any signal.


More information about the PyQt mailing list