Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. memory leak when destroy qquickwidget

memory leak when destroy qquickwidget

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 2 Posters 500 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.
  • T Offline
    T Offline
    tony_yuan
    wrote on last edited by tony_yuan
    #1

    I am using Qt 5.12/Qt5.11, my widget inheirt from qwidiget and embedded an quickwidget in my widget, there is significant memory leak when destroy QuickWidget .
    Below is my widget class:

    class TestPage : public QWidget
    {
        Q_OBJECT
    public:
        explicit TestPage(QWidget *parent = nullptr);
        ~TestPage();
    
    private:
        QQuickWidget *qquickWidget;
        QStackedLayout *l;
    
    signals:
    
    public slots:
    };
    
    TestPage::TestPage(QWidget *parent) : QWidget(parent)
    {
        qquickWidget = nullptr;
        qquickWidget = new QQuickWidget();
        qquickWidget->setSource(QUrl("qrc:/test.qml"));
        l = new QStackedLayout();
        l->addWidget(qquickWidget);
        this->setLayout(l);
    }
    
    TestPage::~TestPage() {
        qDebug() << "destroy TestPage...";
    
        if (qquickWidget) {
            //qquickWidget->engine()->clearComponentCache();
            delete qquickWidget;
            qquickWidget = nullptr;
        }
    
        if (l) {
            delete l;
            l = nullptr;
        }
    }
    
    

    code snip for test,

    TestPage *testPage = new TestPage();
    testPage->show();
    testPage->close();
    testPage->deleteLater();
    

    We notice that memory increase 8M when 500 times new and destroy testPage .

    I create a minimal example project on github:
    https://github.com/tony-yuan-w/test_qquickwidget

    J.HilkJ 1 Reply Last reply
    0
    • T tony_yuan

      I am using Qt 5.12/Qt5.11, my widget inheirt from qwidiget and embedded an quickwidget in my widget, there is significant memory leak when destroy QuickWidget .
      Below is my widget class:

      class TestPage : public QWidget
      {
          Q_OBJECT
      public:
          explicit TestPage(QWidget *parent = nullptr);
          ~TestPage();
      
      private:
          QQuickWidget *qquickWidget;
          QStackedLayout *l;
      
      signals:
      
      public slots:
      };
      
      TestPage::TestPage(QWidget *parent) : QWidget(parent)
      {
          qquickWidget = nullptr;
          qquickWidget = new QQuickWidget();
          qquickWidget->setSource(QUrl("qrc:/test.qml"));
          l = new QStackedLayout();
          l->addWidget(qquickWidget);
          this->setLayout(l);
      }
      
      TestPage::~TestPage() {
          qDebug() << "destroy TestPage...";
      
          if (qquickWidget) {
              //qquickWidget->engine()->clearComponentCache();
              delete qquickWidget;
              qquickWidget = nullptr;
          }
      
          if (l) {
              delete l;
              l = nullptr;
          }
      }
      
      

      code snip for test,

      TestPage *testPage = new TestPage();
      testPage->show();
      testPage->close();
      testPage->deleteLater();
      

      We notice that memory increase 8M when 500 times new and destroy testPage .

      I create a minimal example project on github:
      https://github.com/tony-yuan-w/test_qquickwidget

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @tony_yuan don't call directly delete on QObject/QWidget based classes.
      Either give them -in this case - TestPage as parent and let qt internals handle the cleanup or call deleteLater() on them


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • T Offline
        T Offline
        tony_yuan
        wrote on last edited by tony_yuan
        #3

        thanks for your reply, even I set parent for QQuickWidget and destroy them with deleteLater, there is still significant memory leak. The size of memory leak relate to the qml size.

        While there is no memory leak if i use qquickview to load QML and also embedded into a qwidget.

        TestPage::TestPage(QWidget *parent) : QWidget(parent)
            {
                quickview = nullptr;   
                quickview = new QQuickView();
                QWidget *container = QWidget::createWindowContainer(quickview, this);
                container->setMinimumSize(400, 400);
                container->setMaximumSize(400, 400);
                quickview->setSource(QUrl("qrc:/test.qml"));
                //embedded qquickview to qwidget    
                l = new QStackedLayout(this);    
                l->addWidget(container);
                this->setLayout(l);
            }
        

        May be employee from QT can explain this issue. or only Qt5.12/Qt5.11 have this issue, i will try to install Qt5.15 later.

        J.HilkJ 1 Reply Last reply
        0
        • T tony_yuan

          thanks for your reply, even I set parent for QQuickWidget and destroy them with deleteLater, there is still significant memory leak. The size of memory leak relate to the qml size.

          While there is no memory leak if i use qquickview to load QML and also embedded into a qwidget.

          TestPage::TestPage(QWidget *parent) : QWidget(parent)
              {
                  quickview = nullptr;   
                  quickview = new QQuickView();
                  QWidget *container = QWidget::createWindowContainer(quickview, this);
                  container->setMinimumSize(400, 400);
                  container->setMaximumSize(400, 400);
                  quickview->setSource(QUrl("qrc:/test.qml"));
                  //embedded qquickview to qwidget    
                  l = new QStackedLayout(this);    
                  l->addWidget(container);
                  this->setLayout(l);
              }
          

          May be employee from QT can explain this issue. or only Qt5.12/Qt5.11 have this issue, i will try to install Qt5.15 later.

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #4

          @tony_yuan said in memory leak when destroy qquickwidget:

          May be employee from QT can explain this issue

          maybe, but they usually do not frequent this forum, this is a mainly user based forum :(

          i will try to install Qt5.15 later

          try it, if it's still there and you actually have a minimal example project, show it to us, so we can also test it.

          And that are then also ideal conditions for a bug report over at https://bugreports.qt.io/ :D


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0

          • Login

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