Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Delete vs deleteLater()
Qt 6.11 is out! See what's new in the release blog

Delete vs deleteLater()

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 45.6k Views 2 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.
  • beeckscheB Offline
    beeckscheB Offline
    beecksche
    wrote on last edited by beecksche
    #1

    Hi,
    for me it isn't clear if there is a difference using delete or the deleteLater() slot?

    void Class::create()
    {
       if (m_timer != nullptr) 
          delete m_timer ; // <-- or m_timer->deleteLater()  ?
         
       m_timer = new QTimer();
       connect(m_timer , &QTimer::timeout, this, &Class::process);
       connect(this, &Class::destroyed, m_timer , &QTimer::deleteLater); // delete the member when the class is going to be destroyed
    }
    

    Is there a advantage using deleteLater() in the if-clause?

    Thanks !

    raven-worxR 1 Reply Last reply
    1
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      If you have an event loop and no real memory cluttering then the suggestion is to use deleteLater() so you don't have to worry. If you use delete directly you must make sure that:

      • There are no pending events that m_timer should receive (in this case a timerEvent) or it might crash
      • You must make sure m_timer lives in the same thread as the one you are calling delete from
      • You must make sure the method in which you call delete is not a slot triggerd by the object you are trying to delete or it might crash

      Basically, life is too short to care about all the above and the performance improvement is negligible so using deleteLater() is just better in this case

      P.S.
      connect(this, &Class::destroyed, m_timer , &QTimer::deleteLater); can be replaced by just passing this to the QTimer's constructor

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      beeckscheB 1 Reply Last reply
      5
      • beeckscheB beecksche

        Hi,
        for me it isn't clear if there is a difference using delete or the deleteLater() slot?

        void Class::create()
        {
           if (m_timer != nullptr) 
              delete m_timer ; // <-- or m_timer->deleteLater()  ?
             
           m_timer = new QTimer();
           connect(m_timer , &QTimer::timeout, this, &Class::process);
           connect(this, &Class::destroyed, m_timer , &QTimer::deleteLater); // delete the member when the class is going to be destroyed
        }
        

        Is there a advantage using deleteLater() in the if-clause?

        Thanks !

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @beecksche said in Delete vs deleteLater():

        Is there a advantage using deleteLater() in the if-clause?

        Depends on the use case.

        If you expect that the pointer still receives some event in the current event loop iteration, or shortly after the current execution and want to "mark it as deleted" you use deleteLater()
        Otherwise you can always call delete.

        Another example: if you want to delete a pointer, but still have to pass it to a base-class implementation to let this base class also finish its work you also must call deleteLater.

        But basically if you are unsure try it with delete, if you encounter issues, try deleteLater. If there are still issues there is a bug/misconception in the code.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        4
        • VRoninV VRonin

          If you have an event loop and no real memory cluttering then the suggestion is to use deleteLater() so you don't have to worry. If you use delete directly you must make sure that:

          • There are no pending events that m_timer should receive (in this case a timerEvent) or it might crash
          • You must make sure m_timer lives in the same thread as the one you are calling delete from
          • You must make sure the method in which you call delete is not a slot triggerd by the object you are trying to delete or it might crash

          Basically, life is too short to care about all the above and the performance improvement is negligible so using deleteLater() is just better in this case

          P.S.
          connect(this, &Class::destroyed, m_timer , &QTimer::deleteLater); can be replaced by just passing this to the QTimer's constructor

          beeckscheB Offline
          beeckscheB Offline
          beecksche
          wrote on last edited by beecksche
          #4

          Thanks for the replies and information. I will use the deleteLater() slot!

          @VRonin :

          P.S.
          connect(this, &Class::destroyed, m_timer , &QTimer::deleteLater); can be replaced by just passing this to the QTimer's constructor

          My class Class is moved to another thread and the create function is invoked (to create the members in thread1).

          ...
          class.moveToThread(&thread1);
          ...
          QMetaObject::invokeMethod(&class, "create", Qt::QueuedConnection);
          ...
          

          So if I would use

          m_timer = new QTimer(this);
          

          this (Class object) isn't be created in thread1. And so the parent (this) is not allowed to delete the QTimer object (because it "lives" not in the same thread). Or do I misunderstood this?

          VRoninV 1 Reply Last reply
          0
          • beeckscheB beecksche

            Thanks for the replies and information. I will use the deleteLater() slot!

            @VRonin :

            P.S.
            connect(this, &Class::destroyed, m_timer , &QTimer::deleteLater); can be replaced by just passing this to the QTimer's constructor

            My class Class is moved to another thread and the create function is invoked (to create the members in thread1).

            ...
            class.moveToThread(&thread1);
            ...
            QMetaObject::invokeMethod(&class, "create", Qt::QueuedConnection);
            ...
            

            So if I would use

            m_timer = new QTimer(this);
            

            this (Class object) isn't be created in thread1. And so the parent (this) is not allowed to delete the QTimer object (because it "lives" not in the same thread). Or do I misunderstood this?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @beecksche said in Delete vs deleteLater():

            Or do I misunderstood this?

            if you move class to a second thread then Class::create should be called in the second thread and m_timer can be made child of class the problem is only if you give a parent that lives in another thread, this should not be the case.

            It's at least risky to have class on the stack and move it to the second thread as it might go out of scope and crash the thread that tries to use it.

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            beeckscheB 1 Reply Last reply
            0
            • VRoninV VRonin

              @beecksche said in Delete vs deleteLater():

              Or do I misunderstood this?

              if you move class to a second thread then Class::create should be called in the second thread and m_timer can be made child of class the problem is only if you give a parent that lives in another thread, this should not be the case.

              It's at least risky to have class on the stack and move it to the second thread as it might go out of scope and crash the thread that tries to use it.

              beeckscheB Offline
              beeckscheB Offline
              beecksche
              wrote on last edited by
              #6

              @VRonin

              I followed the thread example in the documentation: http://doc.qt.io/qt-5/qthread.html

              1 Reply Last reply
              0
              • VRoninV Offline
                VRoninV Offline
                VRonin
                wrote on last edited by
                #7

                Which one? did you reimplement run()?

                As you can see Worker *worker = new Worker; which is the equivalent of your class is not on the stack and, although the example doesn't show it, Worker is free to have child objects

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                beeckscheB 1 Reply Last reply
                0
                • VRoninV VRonin

                  Which one? did you reimplement run()?

                  As you can see Worker *worker = new Worker; which is the equivalent of your class is not on the stack and, although the example doesn't show it, Worker is free to have child objects

                  beeckscheB Offline
                  beeckscheB Offline
                  beecksche
                  wrote on last edited by
                  #8

                  @VRonin

                  The first one, I move the class to the thread!

                  In my example I used the wrong operator ".". It should be "->"

                  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