Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-39742

Memory leak in Component.createObject

    XMLWordPrintable

Details

    • 714d9d95484907378b46292df7aec0832f557f1d (qtdeclarative)

    Description

      This Qml code continuously creates objects with Component.createObject() and destroys them immediately afterwards:

      import QtQuick 2.2
      import QtQuick.Window 2.0
      
      Window {
          title: qsTr("Component.createObject Leak Test")
      
          width: 640
          height: 480
      
          property int createdCount;
          property int destroyedCount;
      
          Text {
              text: "Created: " + createdCount + "\nDestroyed: " + destroyedCount
          }
      
          Timer {
              id: timer
      
              interval: 10
              repeat: true
              running: true
      
              onTriggered: {
                  for (var i = 0; i < 40; i++) {
                      var obj = factory.createObject();
                      obj.destroy();
                  }
              }
          }
      
          Component {
              id: factory
              Item {
                  Component.onCompleted: createdCount++
                  Component.onDestruction: destroyedCount++
              }
          }
      }
      

      Memory use should stay constant, but instead it grows continuously. Valgrind's leak check shows this leak:

      ==26587== 2,341,380 (849,000 direct, 1,492,380 indirect) bytes in 7,075 blocks are definitely lost in loss record 4,508 of 4,508
      ==26587==    at 0x402A77B: operator new(unsigned int) (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
      ==26587==    by 0x45F4818: QQmlComponentPrivate::beginCreate(QQmlContextData*) (in /home/schuspe/Qt5.3.0/5.3/gcc/lib/libQt5Qml.so.5.3.0)
      ==26587==    by 0x45F4CA9: QQmlComponent::beginCreate(QQmlContext*) (in /home/schuspe/Qt5.3.0/5.3/gcc/lib/libQt5Qml.so.5.3.0)
      ==26587==    by 0x45F304B: QQmlComponent::createObject(QQmlV4Function*) (in /home/schuspe/Qt5.3.0/5.3/gcc/lib/libQt5Qml.so.5.3.0)
      ==26587==    by 0x46DF128: ??? (in /home/schuspe/Qt5.3.0/5.3/gcc/lib/libQt5Qml.so.5.3.0)
      ==26587==    by 0xBEDA475B: ???
      

      There's only one "new" in QQmlComponentPrivate::beginCreate, at qtdeclarative/src/qml/qml/qqmlcomponent.cpp:883 in the Qt 5.3.0 release:

          state.creator = new QQmlObjectCreator(context, cc, creationContext);
      

      It appears that a new state.creator is being created every time createObject() is called, regardless of whether one had already been created.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            shausman Simon Hausmann
            spencer.schumann Spencer Schumann
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes