From 1304040e5d5af0575cac43aaf1424f72472c7b23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Klitzing?= Date: Tue, 19 May 2020 12:50:35 +0200 Subject: [PATCH] Fix living QObject after shutdown of QCoreApplication QThreadPool is a QObject and must be deleted if the QCoreApplication is being destroyed to release the underlying ThreadData. A Q_GLOBAL_STATIC won't release any memory is not able to manually release it. Pick-to: 5.15 Task-number: QTBUG-84234 Change-Id: Ia82bcff2b564b753ed687f025ff86fa1bed1e64c Reviewed-by: Thiago Macieira --- src/corelib/kernel/qcoreapplication.cpp | 4 +++- src/corelib/thread/qthreadpool.cpp | 11 ++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 87e5d89350a..2894e1a7315 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -892,8 +892,10 @@ QCoreApplication::~QCoreApplication() } QT_CATCH (...) { // swallow the exception, since destructors shouldn't throw } - if (globalThreadPool) + if (globalThreadPool) { globalThreadPool->waitForDone(); + delete globalThreadPool; + } #endif #ifndef QT_NO_QOBJECT diff --git a/src/corelib/thread/qthreadpool.cpp b/src/corelib/thread/qthreadpool.cpp index 9657112abc2..1478cfcb191 100644 --- a/src/corelib/thread/qthreadpool.cpp +++ b/src/corelib/thread/qthreadpool.cpp @@ -40,13 +40,12 @@ #include "qthreadpool.h" #include "qthreadpool_p.h" #include "qdeadlinetimer.h" +#include "qcoreapplication.h" #include QT_BEGIN_NAMESPACE -Q_GLOBAL_STATIC(QThreadPool, theInstance) - /* QThread wrapper, provides synchronization against a ThreadPool */ @@ -478,7 +477,13 @@ QThreadPool::~QThreadPool() */ QThreadPool *QThreadPool::globalInstance() { - return theInstance(); + static QPointer theInstance; + static QBasicMutex theMutex; + + const QMutexLocker locker(&theMutex); + if (theInstance.isNull() && !QCoreApplication::closingDown()) + theInstance = new QThreadPool(); + return theInstance; } /*!