Details
-
Suggestion
-
Resolution: Done
-
P3: Somewhat important
-
None
-
-
1f39b8ba2745f2b9ff462d40f6b0ca473c94484e
Description
For example, I have a QJSEngiene and I want to expose my class Foo to QJSEngine.
class Foo : public QObject { public: Foo(); Q_INVOKABLE void someFunction(); }; class Bar { public: MyClass() { _foo1 = new Foo(); QJSValue jsFoo1 = _jsEngine.newQObject(_foo1); QJSValue jsFoo2 = _jsEngine.newQObject(&_foo2); _jsEngine.globalObject().setProperty("foo1", jsFoo1); _jsEngine.globalObject().setProperty("foo2", jsFoo2); } ~MyClass() { // Here I don't delete _foo1, because It's owned by _jsEngine. } private: QJSEngine _jsEngine; Foo* _foo1; Foo _foo2; }
Code description
Here, following the documentation, the _jsEngine takes the ownership of both objects. Which leeds to a problem when the Bar is destroyed.
The obejct _foo1, which is created dynamically, will be destroyed by QJSEngine, and I can skip deleting it in the desturctor of Bar.
But I can't skip the destroying of _foo2, which is created statically.
I understand that I can manage this question, just creating all objects dynamically. But then I have to handle in my head that "I must not delete it, because it's passed to QJSEngine somewhere else in my code".
And there even can be cases, when creating the object dynamically is undesirable or not possible.
Suggestion
QQmlEngine, which inherits QJSEngine, has a convinient method setObjectOwnership() to handle this quiestion.
I think that it's a good idea to make an option for user to force CppOwnership.
Attachments
Issue Links
- duplicates
-
QTBUG-83657 Moving objectownership from QQmlEngine to QJSEngine in Qt6
- Closed