Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Unsolved Deleting object

    QML and Qt Quick
    3
    6
    1038
    Loading More Posts
    • 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.
    • V
      VeNToR last edited by

      Hi;

      How to delete a object that created by new like follows;

      var myObject;
      myObject = new test(); // or String
      delete myObject;

      This example occures exception.

      Thanks.

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @VeNToR last edited by JonB

        @VeNToR
        I don't use QML, but I feel like throwing my hat into the ring! But assuming this example represents usage of plain JavaScript, you do not use delete against a newed object in the way you would expect in, say, C++.

        The JS delete statement only deletes an object's properties (hence your exception). You cannot delete an object: you have to allow JS to garbage collect them when there are no more references to the object.

        So in your case you can just do nothing and it will get gc-ed on scope exit, or you can just go myObject = null; beforehand if you wish (though it may well not make JS gc it any sooner).

        V 1 Reply Last reply Reply Quote 2
        • V
          VeNToR @JonB last edited by

          @JonB Thank for quick reply. I convert to "test" object to function, and, I have to delete (in my opinion) cause of that it keeps many C++ object inside.

          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @VeNToR last edited by

            @VeNToR
            I'm afraid I don't know what you mean. If you are happy, that's fine, if you want to show your new code, I'll look at it.

            Pablo J. Rogina 1 Reply Last reply Reply Quote 0
            • Pablo J. Rogina
              Pablo J. Rogina @JonB last edited by

              @VeNToR as mentioned before, JavaScript delete operator is not directly related to new operator.

              Please refer to documentation for more details, as your issue seems not related to QML (Qt) itself.

              Upvote the answer(s) that helped you solve the issue
              Use "Topic Tools" button to mark your post as Solved
              Add screenshots via postimage.org
              Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

              1 Reply Last reply Reply Quote 0
              • V
                VeNToR last edited by

                Thanks all ! I am moving codes from js to c++. At this way an other problem, qml can not handle exception from c++ (QJSEngine::evaluate("throw error;").

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post