From e20bf3fbd9ca86eda34b1c3454776ad1e50bc6ea Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 21 Sep 2018 20:20:16 -0700 Subject: [PATCH] Fix persistence of deleted PCs. Fixes #82 --- app/backend/computermanager.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/app/backend/computermanager.cpp b/app/backend/computermanager.cpp index 0f56df17..4c80d7ba 100644 --- a/app/backend/computermanager.cpp +++ b/app/backend/computermanager.cpp @@ -423,9 +423,23 @@ public: void run() { - QWriteLocker lock(&m_ComputerManager->m_Lock); + QThread* pollingThread; - QThread* pollingThread = m_ComputerManager->m_PollThreads[m_Computer->uuid]; + // Only do the minimum amount of work while holding the writer lock. + // We must release it before calling saveHosts(). + { + QWriteLocker lock(&m_ComputerManager->m_Lock); + + pollingThread = m_ComputerManager->m_PollThreads[m_Computer->uuid]; + + m_ComputerManager->m_PollThreads.remove(m_Computer->uuid); + m_ComputerManager->m_KnownHosts.remove(m_Computer->uuid); + } + + // Persist the new host list + m_ComputerManager->saveHosts(); + + // Delete the polling thread if (pollingThread != nullptr) { pollingThread->requestInterruption(); @@ -438,9 +452,8 @@ public: delete pollingThread; } - m_ComputerManager->m_PollThreads.remove(m_Computer->uuid); - m_ComputerManager->m_KnownHosts.remove(m_Computer->uuid); - + // Finally, delete the computer itself. This must be done + // last because the polling thread might be using it. delete m_Computer; }