I "can" call the .destroy or I "have to" call it?
-
Here:
https://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.htmlthere is note about ".destroy" method, but it is not clear for me,
what happened if I do not call.destory
method for object created bycreateObject
?Will the Javascript's garbage collector remove it?
I made experiment, and result is not clear for me.
const dlg = myComponent.createObject(...); dlg.closed.connect(function() { console.debug("!!! destroy dlg"); dlg.destroy(); }); dlg.open();
Sometime I got "Can not call destroy of null",
so somebody already destroyed object,
and if I addComponent.onDestruction
I see that yes,
somebody destroyed myPopup
, beforeclose
signal handler was executed.But who, close method has reference to
dlg
, so "Garbage collector" should
not delete the object and manually I also not destroyed object, because of there are no other destroy calls. -
What are you passing to
createObject
as an argument? Normally there'd be a parent object, and the created object would be kept for as long as the parent is there to reference it. But if you don't supply a parent then the created object would potentially be garbage collected as soon as dlg goes out of scope.I might be misunderstanding your last point, but anyway: it's true that your
dlg.closed
does have a reference todlg
... but it's a circular reference and the garbage collector knows how to deal with those, The real test is whether the object can be reached from the application's root object.Are sure you even need createObject? The pattern for creating popups/dialogs is generally more like as shown in
https://doc.qt.io/qt-5/qml-qtquick-controls2-popup.html -
What are you passing to createObject as an argument?
This is looks like clue. Thanks. It is called from "ListView" delegate,
so I pass delegate as parent.
So may be ListView delegate was deleted before I call "detroy".... but it's a circular reference and the garbage collector knows how to deal with those,
And what QML GC do in this case? As I understand Javscript, this is not direct self reference case,
because of "closure" capture context where "dlg" defined, not "dlg" directly.Are sure you even need createObject?
Yes, Loader is not suitable in my case. Because of I calculate properties in method,
that callscreateObject
. WhileLoader
can calculate properties using properties of surrouned context.
For example, I need pass index value from delegate to createdPopup
.