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. QObject delete order in multi-threaded application
QtWS25 Last Chance

QObject delete order in multi-threaded application

Scheduled Pinned Locked Moved General and Desktop
deletedestroyparentthreads
4 Posts 3 Posters 2.2k Views
  • 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
    s.frings74
    wrote on 26 Jul 2015, 06:38 last edited by s.frings74
    #1

    I found a solution for an issue but I am not sure if that solution is clean and correct.

    My application processes entries of a job queue by multiple concurrent threads. The controller polls the job queue and starts threads. The worker class provides just one single method which is called for each job (concurrently, multi-threaded).

    Worker* worker=new Worker(mainWindow);
    Controller* controller=new Controller(worker,mainWindow);

    When I close the main window, then the destructors of worker and controller are automatically executed, but in the wrong order. The destructor of mainWindow deletes the worker first and then the controller. This is a problem for me, because the threads of the controller are still running and may attempt to call the workers method.

    I need to destruct the controller first, which also terminates the threads. So I put the following line into the constructor of the controller class:

    worker->setParent(this);

    Now the worker gets deleted after the controller, which is the correct order.

    But I am not sure if that is the right solution. Can someone help?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SysTech
      wrote on 26 Jul 2015, 12:31 last edited by
      #2

      Without seeing more of your code it is hard to say.

      What I do see is this:

      You are newing up the controller and worker yourself. I am guessing that you are relying upon going out of scope some how to delete them or are you actively removing them?

      I've heard that delete order is somewhat compiler dependent but generally is the reverse of the order in which you initialize... So in your constructor if you did:

      window::window() :
      controller(nullptr),
      worker(nullptr)
      {
      }
      

      I would expect the compiler to reverse that order on delete. So you could try changing the order in which you initialize.

      On the use of worker->setParent I don't see a problem with doing this way as you are basically assigning ownership of the worker to the controller which is how you seem to want it to be. Again my initial concern is who/what is freeing the controller?

      Someone with more experience might chime in but you also since you are using threads might look into the use of deleteLater() which can take care of removing items when when thread is done.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 26 Jul 2015, 21:16 last edited by
        #3

        Hi,

        @SysTech : deleteLater won't remove things when the thread is done, but at the next event loop iteration.

        @s.frings74 If you need a specific order of deletion, then you should do it yourself to ensure it's done properly. From the looks of it, it's the destruction of your Controller that should drive the destruction of your worker.

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

        S 1 Reply Last reply 27 Jul 2015, 14:16
        1
        • S SGaist
          26 Jul 2015, 21:16

          Hi,

          @SysTech : deleteLater won't remove things when the thread is done, but at the next event loop iteration.

          @s.frings74 If you need a specific order of deletion, then you should do it yourself to ensure it's done properly. From the looks of it, it's the destruction of your Controller that should drive the destruction of your worker.

          S Offline
          S Offline
          SysTech
          wrote on 27 Jul 2015, 14:16 last edited by
          #4

          @SGaist said:

          Hi,

          @SysTech : deleteLater won't remove things when the thread is done, but at the next event loop iteration.

          Thanks. I wasn't exactly sure when it deleted it but good to know!

          1 Reply Last reply
          0

          3/4

          26 Jul 2015, 21:16

          • Login

          • Login or register to search.
          3 out of 4
          • First post
            3/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved