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

Property bindings can be re-evaluated after items are destroyed

    XMLWordPrintable

Details

    • c249edb83fa67b3e5f711b28923397e66876182d

    Description

      Steps to reproduce

      1. Build and run the example project
      2. Click "Reload"

       

      Code

      // SubItem.qml
      import QtQuick 2.15
      
      Item {
          Text {
              anchors.top: parent.top
              text: "SubItem"
      
              onParentChanged: console.log(this, "has a new parent:", parent)
              Component.onCompleted:   console.log("Hello Text!\t", this)
              Component.onDestruction: console.log("G'bye Text!\t", this)
          }
      
          Component.onCompleted:   console.log("Hello Item!\t", this)
          Component.onDestruction: console.log("G'bye Item!\t", this)
      }
      
      // main.qml
      import QtQuick 2.15
      import QtQuick.Window 2.15
      import QtQuick.Controls 2.15
      
      Window {
          id: root
          width: 640
          height: 480
          visible: true
      
          property Component comp: SubItem{}
      
          Column{
              Button {
                  text: "Reload"
                  onClicked: {
                      loader.active = false
                      loader.active = true
                  }
              }
      
              Loader {
                  id: loader
                  sourceComponent: Item {
                      id: loadedItem
                      width: 50
                      height: 50
                      Component.onCompleted: root.comp.createObject(loadedItem)
                  }
              }
          }
      }
      

       

      Outcomes

      At startup:

      qml: QQuickText(0x2c83c0f8250) has a new parent: SubItem_QMLTYPE_3(0x2c83c0f8a30)
      qml: Hello Item!	 SubItem_QMLTYPE_3(0x2c83c0f8a30)
      qml: Hello Text!	 QQuickText(0x2c83c0f8250)
      

       

      Upon clicking "Reload":

      qml: QQuickText(0x2c8425f6aa0) has a new parent: SubItem_QMLTYPE_3(0x2c8425f6740)
      qml: Hello Item!	 SubItem_QMLTYPE_3(0x2c8425f6740)
      qml: Hello Text!	 QQuickText(0x2c8425f6aa0)
      qml: G'bye Text!	 QQuickText(0x2c83c0f8250)
      qml: G'bye Item!	 SubItem_QMLTYPE_3(0x2c83c0f8a30)
      qml: [object Object] has a new parent: undefined
      qrc:/SubItem.qml:5: TypeError: Cannot read property 'top' of undefined
      

      The last 4 lines show that even after the Text object has been destroyed, the Text's `onParentChanged` signal handler still runs and its `anchors.top` property binding is still re-evaluated, which should not happen.

       

      Notes

      Qt 6.3.2 and 6.4.0 are not affected by this issue (The last 2 lines of the output don't appear). It would be good to backport the fix to Qt 5.15 LTS and Qt 6.2 LTS.

      Attachments

        Issue Links

          For Gerrit Dashboard: QTBUG-108028
          # Subject Branch Project Status CR V

          Activity

            People

              semih.yavuz Semih Yavuz
              skoh-qt Sze Howe Koh
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes