I have this code:
QVariant componentFromCode(QString code) {
QQmlComponent * component = new QQmlComponent(engine);
engine->setObjectOwnership(component, QQmlEngine::JavaScriptOwnership);
connect(component, &QQmlComponent::destroyed, this, &Factory::echo);
component->setData(code.toUtf8(), QUrl());
return QVariant::fromValue(component);
}
But it is Factory::echo()never called, which means that the object leaks every time the function is called.
This is what I have on the QML side:
onClicked: {
var code =
'import QtQuick 2.3
Rectangle {
width: 50
height: 50
color: "blue"
}
'
stack.push(Factory.componentFromCode(code))
gc()
}
I explicitly establish the ownership of the object and explicitly call gc()to force garbage collection, but the signal is destroyed()never emitted, so the object is never deleted. From what I read, this should happen automatically in QML.
Please note that it works with:
var comp = Factory.componentFromCode(code)
stack.push(comp)
comp.destroy()
, , , , , , , QML , , / .
EDIT: , , , , , . , :
function JSfoo() {
var obj = CXTProp.getCppQObjectStar()
console.log(obj.objectName)
}
...
QtObject {
property QtObject: CXTProp.getCppQObjectStar()
} // QObject is not collected after the object is destroyed
user3735658