Intermittent crash passing QVariantMap from QML to C++ worker thread
-
I am running into a sporadic crash on Qt 5.12 which appears to be the same as the problem described here: a JS object is passed as the argument of a signal declared in QML and connected to a C++ slot in a worker thread. The C++ slot accepts the argument as a QVariant and converts it into a QVariantMap via
toMap()
:void MyObj::slotVM(QString source, QVariant ur, int) { QVariantMap urvm = ur.toMap();
The program occasionally crashes in the
toMap
function (when it doesn't crash, everything works correctly, as intended). The fix described in the SO post is this:The variant you have there is internally a QJSValue. In order to convert that to a map, we have to call deep into the JavaScript engine, construct scopes, and interact with the JavaScript heap. If any other JavaScript is running at the same time in a different thread, you're in trouble.
Just extract the QVariantMap in the main thread and pass that to your functor as parameter."But does this mean that JS objects can never be simply passed cross-thread from QML to C++? If so, that seems to be a serious caveat that has not been mentioned here: https://doc.qt.io/qt-5/qtqml-cppintegration-data.html#qvariantlist-and-qvariantmap-to-javascript-array-and-object
Is the only solution to "relay" the signal to the worker thread via an intermediate slot-signal pair in C++ in the main thread? Is a QVariantMap argument guaranteed to work within C++ (i.e. passing from a C++ signal in the main thread to C++ slot in worker thread?)