From 4c75456aeeaf6a0100fe40af9c326a468219fd53 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 28 Jun 2018 03:29:22 -0700 Subject: [PATCH] Fix improper handling of polling thread termination --- app/backend/computermanager.cpp | 28 +++++++++++++--------------- app/backend/computermanager.h | 8 +------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/app/backend/computermanager.cpp b/app/backend/computermanager.cpp index 1e73cd37..4e0a2c3a 100644 --- a/app/backend/computermanager.cpp +++ b/app/backend/computermanager.cpp @@ -213,6 +213,10 @@ void ComputerManager::deleteHost(NvComputer* computer) // We must wait here because we're going to delete computer // and we can't do that out from underneath the poller. pollingThread->wait(); + + // The thread is safe to delete now + Q_ASSERT(pollingThread->isFinished()); + delete pollingThread; } m_PollThreads.remove(computer->uuid); @@ -232,8 +236,16 @@ void ComputerManager::stopPollingAsync() while (i.hasNext()) { i.next(); - // The threads will delete themselves when they terminate + QThread* thread = i.value(); + + // Let this thread delete itself when it's finished + connect(thread, SIGNAL(finished()), + thread, SLOT(deleteLater())); + + // The threads will delete themselves when they terminate, + // but we remove them from the polling threads map here. i.value()->requestInterruption(); + i.remove(); } } @@ -291,18 +303,6 @@ bool ComputerManager::addNewHost(QString address, bool mdns) return true; } -void -ComputerManager::handlePollThreadTermination(NvComputer* computer) -{ - QWriteLocker lock(&m_Lock); - - QThread* me = m_PollThreads[computer->uuid]; - Q_ASSERT(me != nullptr); - - m_PollThreads.remove(computer->uuid); - me->deleteLater(); -} - void ComputerManager::handleComputerStateChanged(NvComputer* computer) { @@ -326,8 +326,6 @@ ComputerManager::startPollingComputer(NvComputer* computer) } PcMonitorThread* thread = new PcMonitorThread(computer); - connect(thread, SIGNAL(terminating(NvComputer*)), - this, SLOT(handlePollThreadTermination(NvComputer*))); connect(thread, SIGNAL(computerStateChanged(NvComputer*)), this, SLOT(handleComputerStateChanged(NvComputer*))); m_PollThreads[computer->uuid] = thread; diff --git a/app/backend/computermanager.h b/app/backend/computermanager.h index 60f5f001..e7c0253d 100644 --- a/app/backend/computermanager.h +++ b/app/backend/computermanager.h @@ -156,7 +156,7 @@ private: for (int i = 0; i < TRIES_BEFORE_OFFLINING; i++) { for (auto& address : uniqueAddressList) { if (isInterruptionRequested()) { - goto Terminate; + return; } if (tryPollComputer(address, stateChanged)) { @@ -198,14 +198,10 @@ private: // Wait a bit to poll again QThread::sleep(3); } - - Terminate: - emit terminating(m_Computer); } signals: void computerStateChanged(NvComputer* computer); - void terminating(NvComputer* computer); private: NvComputer* m_Computer; @@ -235,8 +231,6 @@ signals: private slots: void handleComputerStateChanged(NvComputer* computer); - void handlePollThreadTermination(NvComputer* computer); - private: void saveHosts();