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. Using deleteLater() without main event loop
Forum Updated to NodeBB v4.3 + New Features

Using deleteLater() without main event loop

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 772 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.
  • S Offline
    S Offline
    sykac
    wrote on last edited by
    #1

    Hello,
    I'm trying to properly stop and delete a qthread and its worker. The thread and the worker were created in a main application thread where no event loop is running.

    thread_ = new QThread(this),
    worker_ = new ActiveDevicePrivate(*this);
    worker_->moveToThread(thread_);
    thread_->start();
    

    Later I want to stop the thread and delete it together with the worker.

    thread_->quit();
    thread_->wait();
    
    thread_->deleteLater();
    worker_->deleteLater();
    

    But as I read the documentation I found this: If deleteLater() is called after the main event loop has stopped, the object will not be deleted. I assume that this is also my case (as after the main loop stopped or there is no main loop are probably the same things). Am I right? Using this code my objects won't be deleted? If that's true, how can I delete them properly? I tried to use just delete thread_, but it crashes the application.

    VRoninV 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Since you're passing this as parameter of your QThread constructor, you don't seem to call in your main function, do you ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • S Offline
        S Offline
        sykac
        wrote on last edited by
        #3

        @SGaist No, it is not in my main function. The thread is a member of an object which lives in the main thread. Is that important somehow?

        1 Reply Last reply
        0
        • S sykac

          Hello,
          I'm trying to properly stop and delete a qthread and its worker. The thread and the worker were created in a main application thread where no event loop is running.

          thread_ = new QThread(this),
          worker_ = new ActiveDevicePrivate(*this);
          worker_->moveToThread(thread_);
          thread_->start();
          

          Later I want to stop the thread and delete it together with the worker.

          thread_->quit();
          thread_->wait();
          
          thread_->deleteLater();
          worker_->deleteLater();
          

          But as I read the documentation I found this: If deleteLater() is called after the main event loop has stopped, the object will not be deleted. I assume that this is also my case (as after the main loop stopped or there is no main loop are probably the same things). Am I right? Using this code my objects won't be deleted? If that's true, how can I delete them properly? I tried to use just delete thread_, but it crashes the application.

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

          @sykac said in Using deleteLater() without main event loop:

          worker_->deleteLater();

          Always be careful with calling direct methods of an item that lives in a different thread. It's safer to connect(thread_,&QThread::finished,worker_,&QObject::deleteLater); just before you call thread_->quit();. The worker will be cleaned up by the secondary thread event loop.

          thread_->deleteLater();

          this will not work if you don't have an event loop in the main thread but, as @SGaist mentioned, if you pass a parent to it in the constructor it will get automatically deleted when the parents gets deleted/goes out of scope

          "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

          1 Reply Last reply
          2
          • S Offline
            S Offline
            sykac
            wrote on last edited by
            #5

            @VRonin said in Using deleteLater() without main event loop:

            It's safer to connect(thread_,&QThread::finished,worker_,&QObject::deleteLater); just before you call thread_->quit();.

            I thought that's why thread_->wait() is called. I'll try to use the connect.

            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