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. I "can" call the .destroy or I "have to" call it?
Forum Updated to NodeBB v4.3 + New Features

I "can" call the .destroy or I "have to" call it?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 2 Posters 370 Views 1 Watching
  • 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.
  • D Offline
    D Offline
    DaveMilter
    wrote on last edited by
    #1

    Here:
    https://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

    there is note about ".destroy" method, but it is not clear for me,
    what happened if I do not call .destory method for object created by createObject?

    Will the Javascript's garbage collector remove it?

    I made experiment, and result is not clear for me.

    const dlg = myComponent.createObject(...);
    dlg.closed.connect(function() {
        console.debug("!!! destroy dlg");
        dlg.destroy();
    });
    dlg.open();
    

    Sometime I got "Can not call destroy of null",
    so somebody already destroyed object,
    and if I add Component.onDestruction I see that yes,
    somebody destroyed my Popup, before close signal handler was executed.

    But who, close method has reference to dlg, so "Garbage collector" should
    not delete the object and manually I also not destroyed object, because of there are no other destroy calls.

    timdayT 1 Reply Last reply
    1
    • D DaveMilter

      Here:
      https://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

      there is note about ".destroy" method, but it is not clear for me,
      what happened if I do not call .destory method for object created by createObject?

      Will the Javascript's garbage collector remove it?

      I made experiment, and result is not clear for me.

      const dlg = myComponent.createObject(...);
      dlg.closed.connect(function() {
          console.debug("!!! destroy dlg");
          dlg.destroy();
      });
      dlg.open();
      

      Sometime I got "Can not call destroy of null",
      so somebody already destroyed object,
      and if I add Component.onDestruction I see that yes,
      somebody destroyed my Popup, before close signal handler was executed.

      But who, close method has reference to dlg, so "Garbage collector" should
      not delete the object and manually I also not destroyed object, because of there are no other destroy calls.

      timdayT Offline
      timdayT Offline
      timday
      wrote on last edited by
      #2

      @DaveMilter

      What are you passing to createObject as an argument? Normally there'd be a parent object, and the created object would be kept for as long as the parent is there to reference it. But if you don't supply a parent then the created object would potentially be garbage collected as soon as dlg goes out of scope.

      I might be misunderstanding your last point, but anyway: it's true that your dlg.closeddoes have a reference to dlg... but it's a circular reference and the garbage collector knows how to deal with those, The real test is whether the object can be reached from the application's root object.

      Are sure you even need createObject? The pattern for creating popups/dialogs is generally more like as shown in
      https://doc.qt.io/qt-5/qml-qtquick-controls2-popup.html

      D 1 Reply Last reply
      0
      • timdayT timday

        @DaveMilter

        What are you passing to createObject as an argument? Normally there'd be a parent object, and the created object would be kept for as long as the parent is there to reference it. But if you don't supply a parent then the created object would potentially be garbage collected as soon as dlg goes out of scope.

        I might be misunderstanding your last point, but anyway: it's true that your dlg.closeddoes have a reference to dlg... but it's a circular reference and the garbage collector knows how to deal with those, The real test is whether the object can be reached from the application's root object.

        Are sure you even need createObject? The pattern for creating popups/dialogs is generally more like as shown in
        https://doc.qt.io/qt-5/qml-qtquick-controls2-popup.html

        D Offline
        D Offline
        DaveMilter
        wrote on last edited by
        #3

        @timday

        What are you passing to createObject as an argument?

        This is looks like clue. Thanks. It is called from "ListView" delegate,
        so I pass delegate as parent.
        So may be ListView delegate was deleted before I call "detroy".

        ... but it's a circular reference and the garbage collector knows how to deal with those,

        And what QML GC do in this case? As I understand Javscript, this is not direct self reference case,
        because of "closure" capture context where "dlg" defined, not "dlg" directly.

        Are sure you even need createObject?

        Yes, Loader is not suitable in my case. Because of I calculate properties in method,
        that calls createObject. While Loader can calculate properties using properties of surrouned context.
        For example, I need pass index value from delegate to created Popup.

        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