From 39b6305e2e8b874fd6d1f20755e273a402e47831 Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Mon, 10 Oct 2022 20:44:25 +0100 Subject: [PATCH] Delete worker threads in a less convoluted manner --- src/blackmisc/worker.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/blackmisc/worker.cpp b/src/blackmisc/worker.cpp index 712f90895..72ee69cce 100644 --- a/src/blackmisc/worker.cpp +++ b/src/blackmisc/worker.cpp @@ -98,12 +98,12 @@ namespace BlackMisc // MS 2018-09 Now we post the DeferredDelete event from within the worker thread, but rely on it being dispatched // by the owner thread. Posted events are moved along with the object when moveToThread is called. - // We also connect its destroyed signal to delete the worker thread at the same time. this->deleteLater(); - connect(this, &QObject::destroyed, workerThread, &QObject::deleteLater); this->moveToThread(workerThread->thread()); // move worker back to the thread which constructed it, so there is no race on deletion // must not access the worker beyond this point, as it now lives in the owner's thread and could be deleted at any moment + + workerThread->deleteLater(); } CWorkerBase::CWorkerBase() @@ -272,15 +272,16 @@ namespace BlackMisc { this->setFinished(); + QThread *workerThread = this->thread(); Q_ASSERT_X(m_owner->thread()->isRunning(), Q_FUNC_INFO, "Owner thread's event loop already ended"); // MS 2018-09 Now we post the DeferredDelete event from within the worker thread, but rely on it being dispatched // by the owner thread. Posted events are moved along with the object when moveToThread is called. - // We also connect its destroyed signal to delete the worker thread at the same time. this->deleteLater(); - connect(this, &QObject::destroyed, this->thread(), &QObject::deleteLater); this->moveToThread(m_owner->thread()); // move worker back to the thread which constructed it, so there is no race on deletion // must not access the worker beyond this point, as it now lives in the owner's thread and could be deleted at any moment + + workerThread->deleteLater(); } } // ns