QJSEngine cleanup/destructor
-
Did you already check the bug report system for something related ?
-
@SGaist Yeah, I looked around, nobody seems to have the same problem. Should I report it there?
Also, just to clarify: I should be able to create and destroy QJSEngine objects this way? So it's not like I'm trying to make the framework do something it's not supposed to?
-
Although I haven't used that class extensively, I am not aware of such a limitation. So go on and create a report. Please provide a minimal compilable example.
-
@SGaist While making the minimal compilable example, I managed to find the actual source of the problem. It wasn't the destructor at all, at least not directly, but instead me exposing my "interface" to JavaScript. Below is a small example:
QJSEngine* jsEngine = new QJSEngine(); // Add ourselves to the JS engine QJSValue interface = jsEngine->newQObject(interfacePtr); // Expose interface in the global namespace jsEngine->globalObject().setProperty("INTERFACE", interface); // Do stuff... delete jsEngine; // <-- error happens here
It seems that using
newQObject
causes trouble once I try destroying the QJSEngine, and the documentation references this as well:If the given object is deleted outside of the engine's control, any attempt to access the deleted QObject's members through the JavaScript wrapper object (either by script code or C++) will result in a script exception.
So I suspect I need to "clean up" this object before deleting the engine, otherwise (presumably) some ownership issues come up when the JS heap is cleaned up. Unfortunately, I have no idea how to fix this. I tried using
deleteProperty
on the global object, but that did not work. -
-
@yah_nosh said in QJSEngine cleanup/destructor:
Okay, found the solution. I needed to change the ownership using
QQmlEngine::setObjectOwnership
, and the JS engine stops trying to delete my objects. So technically there was no bug at all, but the source code and documentation sorely needs an update.You should consider improving the documentation and submit a patch for that so everybody can benefit from the outcome of your findings.
-
Anyone can contribute to the documentation, the code, etc. You have to follow the procedures and rules but it's not some walled garden.