Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. updatePaintNode memory leak
Forum Updated to NodeBB v4.3 + New Features

updatePaintNode memory leak

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 3 Posters 290 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    ssssw
    wrote on last edited by
    #1

    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;
    
    }
    
    jsulmJ 1 Reply Last reply
    0
    • S ssssw

      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;
      
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      GrecKoG 1 Reply Last reply
      3
      • jsulmJ jsulm

        @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.

        GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        Heaptrack is an easy to use alternative to Valgrind (and it's faster).

        1 Reply Last reply
        1
        • S ssssw has marked this topic as solved on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved