I have a QML component with a property and a default value. If I instantiate this component and overriding the value of the component, the previous, default value is not destroyed.
Example:
The QML component, MyComponent.qml
import QtQuick 2.12
Item {
property MyResource resource: MyResource {
name: "B"
}
}
MyResource.qml
import QtQuick 2.12
QtObject {
property string name: "Default"
Component.onCompleted: console.log("Created MyResource", name, this);
Component.onDestruction: console.log("Destroyed MyResource", name, this);
}
And Root.qml to instantiate the component:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
width: 300
height: 300
MyComponent {
resource: MyResource {
name: "A"
}
}
}
Output of qml Root.qml:
qml: Created MyResource A MyResource_QMLTYPE_0(0x5645cc25b770)
qml: Created MyResource B MyResource_QMLTYPE_0(0x5645cc2a1f60)
The default value (the MyResource with the name "B") is never deleted. This behavior is the same when using a C++ QObject instead of MyResource. Manually calling the garbage collector (QJsEngine::collectGarbage) doesn't help.
I tried in Qt 5.15 and Qt 6.4.2.
How can I get rid of the default value of the property?