From 74fdd97a5204800e85ea77bb6b46efa865ff2d0a Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Thu, 7 Aug 2014 22:19:15 +0200 Subject: [PATCH] CJob: Even cancel finished jobs When a job was cancelled after its runThread() method finished, but before the main thread noticed this and reacted, we would just run runMain() before and pretend the job finished normally. However, with CModuleJob this means that runMain() might get called for a module which is currently being destructed. This has bad effects with virtual functions and thus causes problems. It's better to just really cancel the job instead. Signed-off-by: Uli Schlachter --- src/Threads.cpp | 8 ++++++-- test/ThreadTest.cpp | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Threads.cpp b/src/Threads.cpp index 492056cb..a49eab9e 100644 --- a/src/Threads.cpp +++ b/src/Threads.cpp @@ -187,6 +187,7 @@ void CThreadPool::cancelJobs(const std::set &jobs) { continue; case CJob::DONE: + (*it)->m_eState = CJob::CANCELLED; finished.insert(*it); continue; @@ -226,8 +227,11 @@ void CThreadPool::cancelJobs(const std::set &jobs) { // Handle finished jobs. They must already be in the pipe. while (!finished.empty()) { CJob *job = getJobFromPipe(); - finishJob(job); - finished.erase(job); + if (finished.erase(job) > 0) { + assert(job->m_eState == CJob::CANCELLED); + delete job; + } else + finishJob(job); } // Delete things that still need to be deleted diff --git a/test/ThreadTest.cpp b/test/ThreadTest.cpp index 8e7a65ad..8346b21b 100644 --- a/test/ThreadTest.cpp +++ b/test/ThreadTest.cpp @@ -154,7 +154,7 @@ public: } ~CEmptyJob() { - EXPECT_FALSE(wasCancelled()); + EXPECT_TRUE(wasCancelled()); m_bDestroyed = true; }