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

Incubated object is garbage collected before a reference to it can be created

    XMLWordPrintable

Details

    • All
    • 95965ba19 (dev), 5723be147 (6.7), a88a9f494 (6.6), 1cc338f81 (tqtc/lts-6.5), 0709d83dc (dev)

    Description

      When instantiating an object with Component.incubateObject and a null parent, the incubated object might be garbage collected before there is a chance to make a strong reference to it, as documented in https://doc.qt.io/qt-6/qml-qtqml-component.html#incubateObject-method

       The patch in QTBUG-53111 does avoid the issue when the object is instantiated asynchronously, where in this case a reference to the object is kept until after the status is notified with statusChanged.

      But if the object is instantiated right away, it will already be destroyed right after the incubateObject call.

      This can be reproduced easily with the following code and QV4_MM_AGGRESSIVE_GC=1:

      import QtQuick 2.0
      Item {
          Component {
              id: comp
              Item {
                  Component.onDestruction: print("DESTROY")
                  Component.onCompleted: print("COMPLETED")
              }
          }
      
          Component.onCompleted: {
              var i = comp.incubateObject(null, {}, Qt.Asynchronous);
              print("incubate called");
      
              i.onStatusChanged = () => {
                  if (i.status == Component.Ready) {
                      console.assert(i.object, "incubate failed: object is", i.object);
                  }
              };
      
              if (i.status == Component.Ready)
                  console.assert(i.object, "incubate failed: object is", i.object);
          }
      }
      

      Attachments

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

        Activity

          People

            ulherman Ulf Hermann
            rawoul Arnaud Vrac
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: