Track worker construction and destruction

Added debug log messages in case a worker still exists when the `qApp` is destroyed.
This would mean that a `quitAndWait` is missing or is being skipped somewhere.
This commit is contained in:
Mat Sutcliffe
2020-03-28 22:09:25 +00:00
parent e5528288b5
commit 2f1e3f47f9
3 changed files with 30 additions and 0 deletions

View File

@@ -173,6 +173,14 @@ namespace BlackCore
m_inputManager = new CInputManager(this); m_inputManager = new CInputManager(this);
m_inputManager->createDevices(); m_inputManager->createDevices();
} }
connect(this, &QObject::destroyed, [cat = CLogCategoryList(this)]
{
for (CWorkerBase *worker : CWorkerBase::allWorkers())
{
CLogMessage(cat).debug(u"Worker named '%1' still exists after application destroyed") << worker->objectName();
}
});
} }
} }

View File

@@ -22,6 +22,8 @@
namespace BlackMisc namespace BlackMisc
{ {
QSet<CWorkerBase *> CWorkerBase::s_allWorkers;
void CRegularThread::run() void CRegularThread::run()
{ {
#ifdef Q_OS_WIN32 #ifdef Q_OS_WIN32
@@ -105,6 +107,16 @@ namespace BlackMisc
// must not access the worker beyond this point, as it now lives in the owner's thread and could be deleted at any moment // must not access the worker beyond this point, as it now lives in the owner's thread and could be deleted at any moment
} }
CWorkerBase::CWorkerBase()
{
s_allWorkers.insert(this);
}
CWorkerBase::~CWorkerBase()
{
s_allWorkers.remove(this);
}
const CLogCategoryList &CWorkerBase::getLogCategories() const CLogCategoryList &CWorkerBase::getLogCategories()
{ {
static const BlackMisc::CLogCategoryList cats { CLogCategory::worker() }; static const BlackMisc::CLogCategoryList cats { CLogCategory::worker() };

View File

@@ -163,6 +163,9 @@ namespace BlackMisc
//! Convenience to call abandon() followed by waitForFinished(). //! Convenience to call abandon() followed by waitForFinished().
void abandonAndWait() noexcept; void abandonAndWait() noexcept;
//! All workers currently existing.
static const QSet<CWorkerBase *> &allWorkers() { return s_allWorkers; }
signals: signals:
//! Emitted when the task is about to start. //! Emitted when the task is about to start.
void aboutToStart(); void aboutToStart();
@@ -172,6 +175,12 @@ namespace BlackMisc
void finished(); void finished();
protected: protected:
//! Constructor.
CWorkerBase();
//! Destructor.
virtual ~CWorkerBase() override;
//! For the task to check whether it can finish early. //! For the task to check whether it can finish early.
//! \threadsafe //! \threadsafe
bool isAbandoned() const; bool isAbandoned() const;
@@ -197,6 +206,7 @@ namespace BlackMisc
bool m_started = false; bool m_started = false;
bool m_finished = false; bool m_finished = false;
mutable QMutex m_finishedMutex { QMutex::Recursive }; mutable QMutex m_finishedMutex { QMutex::Recursive };
static QSet<CWorkerBase *> s_allWorkers;
}; };
/*! /*!