[PyQt] Segfault in QMenu.addAction when parent widget is deleted.

Yuya Nishihara yuya at tcha.org
Sat Apr 5 05:01:22 BST 2014


On Wed, 2 Apr 2014 13:11:05 -0700, J Robert Ray wrote:
> Hi all, I wanted to share this small example program that will segfault.
> 
> The newest PyQt I have been able to test it with PyQt 4.10 / Qt 4.8.4, so I
> don't know if it has been fixed already.
> 
> In boils down to trying to add a new action to a submenu of a menu attached
> to a widget that has been deleted. We're having this problem due to some
> asynchronous RPC and getting results back after a user has caused the
> widget to go away.
> 
> If anyone can try this in a newer PyQt to see if it still crashes, I would
> appreciate it.

No difference in PyQt 4.10.3 with Qt 4.8.5.

>     w = QtGui.QWidget()
>     m = QtGui.QMenu(w)
>     sub = m.addMenu('Sub')
>     def cb():
>         # Using 'm' instead of 'sub' here would instead raise
>         # RuntimeError: underlying C/C++ object has been deleted
>         sub.addAction('Foo', lambda x: x)
> 
>     QtCore.QTimer.singleShot(100, cb)
> 
>     # Comment next line to stop crashing.
>     del w

It fails because Qt manages lifetime of objects in tree.  It would be nice
if RuntimeError can be raised in that case, but I guess m.addMenu() does not
create a safety net for segfaults.

http://qt-project.org/doc/qt-4.8/objecttrees.html#construction-destruction-order-of-qobjects

Regards,


More information about the PyQt mailing list