[PyQt] pyqt5 segfault when passing callback functions
Phil Thompson
phil at riverbankcomputing.com
Wed Nov 5 10:48:35 GMT 2014
On 03/11/2014 12:32 am, Ben wrote:
> I'm trying to pass a callback function from QML/JS to python and back
> again. (Basically, JS tells python to do X and run this (JS) function
> when X returns). Unfortunately, this causes segfaults. I'm using pyqt5
> with either python 2.7 or 3.4 with the same results. Tweaking the code
> (adding / removing log lines, etc) can change the behavior so I'm
> thinking that some garbage collection on the QT/QML/JS side is
> deleting the function from memory, but python is keeping the (now
> invalid) function pointer and executing it, with inconsistent results.
> I can't find any example code for doing this with pyqt5/QML (we have a
> c++ qt5 implementation that works) but seems like a common thing to
> do. Is there a better approach? Or just something dumb I'm doing?
>
> I wrote a very simple program that segfaults and shows my problem. It
> is also available on https://github.com/ben-github/pyqt5-segfault and
> I'll also show it inline below. When I run the code I get:
>
> Starting ICE Control GUI...
> qml: onLoad start==================
> Enqueuing function of #version 1
> Callback is <PyQt5.QtQml.QJSValue object at 0x7f213dd05278>
> Callback is callable <built-in method isCallable of QJSValue object at
> 0x7f213dd05278>
> Callback is callable False
> qml: =========================
> processing responses
> Callback is <PyQt5.QtQml.QJSValue object at 0x7f213dd05278>
> Callback is callable <built-in method isCallable of QJSValue object at
> 0x7f213dd05278>
> Segmentation fault
>
> The segfault is from the line in main.py:
> print('Callback is callable %s' % self.callback.isCallable())
>
> where self.callback is the JS callback function.
QJSValue is a value class so you need to make a copy of the callback
argument of the enqueue() method...
self.callback = QJSValue(callback)
Phil
More information about the PyQt
mailing list