diff --git a/src/presentation/applicationmodel.h b/src/presentation/applicationmodel.h --- a/src/presentation/applicationmodel.h +++ b/src/presentation/applicationmodel.h @@ -50,6 +50,7 @@ typedef QSharedPointer Ptr; explicit ApplicationModel(QObject *parent = Q_NULLPTR); + ~ApplicationModel(); QObject *availableSources(); QObject *availablePages(); diff --git a/src/presentation/applicationmodel.cpp b/src/presentation/applicationmodel.cpp --- a/src/presentation/applicationmodel.cpp +++ b/src/presentation/applicationmodel.cpp @@ -31,6 +31,7 @@ #include "presentation/pagemodel.h" #include "utils/dependencymanager.h" +#include "utils/jobhandler.h" using namespace Presentation; @@ -41,6 +42,11 @@ MetaTypes::registerAll(); } +ApplicationModel::~ApplicationModel() +{ + Utils::JobHandler::clear(); +} + QObject *ApplicationModel::availableSources() { if (!m_availableSources) { diff --git a/src/utils/jobhandler.h b/src/utils/jobhandler.h --- a/src/utils/jobhandler.h +++ b/src/utils/jobhandler.h @@ -44,6 +44,8 @@ void install(KJob *job, const ResultHandler &handler, StartMode startMode = AutoStart); void install(KJob *job, const ResultHandlerWithJob &handler, StartMode startMode = AutoStart); + void clear(); + int jobCount(); } diff --git a/src/utils/jobhandler.cpp b/src/utils/jobhandler.cpp --- a/src/utils/jobhandler.cpp +++ b/src/utils/jobhandler.cpp @@ -77,6 +77,22 @@ job->start(); } +template +void clearJobs(JobHandlerInstance *self, QHash> &jobs) +{ + foreach (auto *job, jobs.keys()) { + QObject::disconnect(job, 0, self, 0); + } + jobs.clear(); +} + +void JobHandler::clear() +{ + auto self = jobHandlerInstance(); + clearJobs(self, self->m_handlers); + clearJobs(self, self->m_handlersWithJob); +} + int JobHandler::jobCount() { auto self = jobHandlerInstance(); diff --git a/tests/units/presentation/applicationmodeltest.cpp b/tests/units/presentation/applicationmodeltest.cpp --- a/tests/units/presentation/applicationmodeltest.cpp +++ b/tests/units/presentation/applicationmodeltest.cpp @@ -24,6 +24,7 @@ #include #include "utils/dependencymanager.h" +#include "utils/jobhandler.h" #include "utils/mockobject.h" #include "presentation/applicationmodel.h" @@ -211,6 +212,21 @@ QCOMPARE(editor->errorHandler(), &errorHandler2); QCOMPARE(page->errorHandler(), &errorHandler2); } + + void shouldClearJobHandlersOnExit() + { + // GIVEN + auto app = new Presentation::ApplicationModel; + Utils::JobHandler::install(new FakeJob, [] { qFatal("Shouldn't happen"); }); + QCOMPARE(Utils::JobHandler::jobCount(), 1); + + // WHEN + delete app; + + // THEN + QCOMPARE(Utils::JobHandler::jobCount(), 0); + QTest::qWait(FakeJob::DURATION * 2); + } }; QTEST_MAIN(ApplicationModelTest)