I using a QSGGeometry
, QSGVertexColorMaterial
& a QSGGeometryNode
to draw something real time on my QQuickItem
derived class that is MyQuickItem
here.
Following is my updatePaintNode
method where the crux of the repaint logic lies.
QSGNode * MyQuickItem::updatePaintNode(QSGNode * oldNode, UpdatePaintNodeData * updatePaintNodeData) {
if (!oldNode) {
oldNode = new QSGNode;
}
oldNode->removeAllChildNodes();
QSGGeometry * geometry = GetMyGeometry();
QSGVertexColorMaterial * material = new QSGVertexColorMaterial;
QSGGeometryNode * child_node = new QSGGeometryNode;
child_node->setGeometry(geometry);
child_node->setMaterial(material);
child_node->setFlag(QSGNode::OwnsMaterial);
oldNode->appendChildNode(child_node);
return oldNode;
}
Issue:
Above logic works great. No problem with functionality at all. No performance issues as well. But I worried that I am causing memory leaks. Look at following two lines in the above method updatePaintNode
where I allocate raw pointers.
QSGVertexColorMaterial * material = new QSGVertexColorMaterial;
QSGGeometryNode * child_node = new QSGGeometryNode;
I allocate them & I do not delete them. This is because the point where they should be deleted is after updatePaintNode
finishes. And thats not under my control.
Question:
How can I ensure that the 2 pointers material
& child_node
are cleared from memory correctly?
Does doing a child_node->setFlag(QSGNode::OwnsMaterial)
like I am doing above sets the ownership of the pointers to QtSceneGraph & relieve me of the burden of deleting the pointers?
Secondary Question:
I am using oldNode->removeAllChildNodes()
to clear the data drawn in the previous frame. Is this a good way to clear previous data on screen before painting new data?
PS:
I reiterate: There are no performance issues with this implementation. I just want to be sure that I am not causing any memory leaks. I have tried using material
& child_node
as smart pointers like so:
auto material = std::make_shared<QSGVertexColorMaterial>();
auto child_node = new std::make_shared<QSGGeometryNode>();
But this causes a crash when material
& child_node
are auto-cleared from memory at a later point.