updatePaintNode memory leak
-
Hello,
I am implementing a drawing function on the Windows platform using QML and have encountered an issue. I use updatePaintNode to perform a layered drawing function, and I control the execution of layer drawing through the UI by calling update(). During the drawing process of each layer, I delete the node data from the previous layer and then recreate a QSGGeometryNode to draw new data.
I have noticed the following problem: for example, when I draw from layer 0 up to layer 20, the memory usage gradually increases and peaks at layer 20. When I control the drawing from layer 20 back down to layer 0, the memory does not decrease to the state it was when I first drew to layer 0, and it remains roughly the same as it was at layer 20. If I repeat the drawing process from layer 0 to 20 again, there is no significant change in memory usage; it basically stays at the state it was during my first drawing at layer 20.
Is my code causing a memory leak, or does QML cache previous data to speed up subsequent redraws? If it is the latter, is there a way to clear these caches? Because if the number of layers is too high, it could consume a significant amount of memory.
QSGNode* CMOpenGLItem::updatePaintNode(QSGNode* oldNode, QQuickItem::UpdatePaintNodeData*) { if (m_controller->GetLayer() == nullptr) return nullptr; QSGTransformNode* parentNode = static_cast<QSGTransformNode*>(oldNode); if (!parentNode) { parentNode = new QSGTransformNode(); parentNode->setFlag(QSGNode::OwnsGeometry); } while (auto child= parentNode->firstChild()) { parentNode->removeChildNode(child); delete child; } std::vector<int> vLineType; QVector<QVector<QPointF>> polylineVertices = GetPolylineDataOfOneLayer(m_controller->GetLayer(), vLineType); for (int i = 0; i < polylineVertices.size(); ++i) { QSGGeometryNode* node = new QSGGeometryNode; auto geo= createGeometry(polylineVertices[i], vLineType[i]); node->setFlag(QSGNode::OwnsGeometry); node->setGeometry(geo); if (vLineType[i] == 3) { node->setMaterial(m_blueMat); } else { node->setMaterial(m_redMat); } parentNode->appendChildNode(node); } return parentNode; }
-
@ssssw What you describe does not sound like a memory leak. If your application releases memory it can stay "assigned" to your application, so next time you allocate memory the allocation is faster. This is up to the OS to decide.
To check for memory leaks you should use proper tools like Valgrind. -