If I have some object, Item or QtObject as a property of a QML element (let's say Item as the container, but I am also interested in the situation, when the containing object is QtObject), how is the memory management done?
Think about these following situations:
1: property var someObject: { "key" : "value" }
2: property Item someItem: Item { ... }
3: property QtObject someQtObject: QtObject { ... }
Will the containing element be the parent? Will the memory of the property object be released, when the parent gets destroyed? Is this actually not a good thing to do, and might lead to memory leaks, unless the properties are deleted or released in code? And so on. Any insights?
Also, would it be beneficial to do something like this in such situation:
4: property Item someItem: Item {
parent: containingElementId
...
}
I have sometimes the need to do things such as here, and not simply configure the object to be under the children default property of an item. Also, when the containing object is QtObject, there is no children default property, but memory still needs to be managed.
property Item someItem: Item { ... }
The item won't be parented, but it will be collected nevertheless. Setting a parent explicitly will ensure that it is visible in the respective parent.
Keep in mind that Item::parent
is the visual parent, not the actual parent. So even if you explicitly set a parent that survives longer than the property holder object, the property value object will be collected nevertheless.
This is because unlike the visual parent that remains null, the "logical" parent object will be set to the property holder object.
QML's memory management is automatic and for most parts works as expected. Two things to keep in mind:
So basically:
it's most likely ok, but even if it isn't there is nothing you can do about it, as there is no explicit deletion of JS objects
will be collected but will not be visible
will be collected (keep in mind QtObject doesn't expose any parent)
will be collected but will also be visible, a surviving parent will not save it from deletion