[PyQt] segfault when using qml (probably reference counting error)

Phil Thompson phil at riverbankcomputing.com
Fri Mar 18 16:20:23 GMT 2011

On Fri, 18 Mar 2011 10:54:16 -0500, Rex Dieter <rdieter at math.unl.edu>
> Quoting downstream bug report,
> https://bugzilla.redhat.com/show_bug.cgi?id=676042
> (Using PyQt-4.8.3)...
> When running some simple PyQt4 + QML apps, a segfault can be produced
> the QML tries to make use of an object that is exposed to it by the PyQt

> code. Changing the code very slightly so that the PyQt4 code has an
> reference to the object removes the segfault.  This likely means that a 
> reference to the object is not being created in the PyQt4 declarative
> the downstream bug also includes some sample code as a test-case.

It's a feature, not a bug...

PyQt correctly gives an extra reference to the Stopwatch context object to
the QDeclarativeContext returned by rootContext() to prevent it being
garbage collected. However the QDeclarativeContext Python object (not the
C++ instance) is itself garbage collected and so the extra reference is
then discarded.

Therefore you need to keep an explicit reference to the Stopwatch instance
(as in your workaround), or the value returned by rootContext(), or give
the Stopwatch instance a sensible parent.


More information about the PyQt mailing list