summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRobert Griebl <robert.griebl@qt.io>2023-08-10 01:55:52 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2023-08-16 12:02:31 +0000
commitd486d676bb12cb65321a0dde68df7d08fffef57d (patch)
tree25938603e2eaa186d1fee3629fa3c07dbda2bed3
parente56fdb7a4b209ff08eea32ca5c2183db0e3d0e28 (diff)
Fix logging after static destructors have run
Before we could either crash or end up in an infinite loop (*) when somebody logged after the static QThreadStorage in the colorLogToStderr() had been destructed. (*) QThreadStorageData::finish() can potential log on errors, which reallocted the outBuffers, which messed up the tls list, which led to finish() deleting the buffers and then logging yet another error... [this is for 6.6/6.5 only - 6.7 gets a similar, but simpler fix] Change-Id: I47f0b403b214b42e3b68f5ecc4776fa6aaba0cfc Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Thomas Senyk <thomas.senyk@qt.io> (cherry picked from commit 840a9b43704ad8e32af18297bba207667dfa815a) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/common-lib/logging.cpp11
1 files changed, 8 insertions, 3 deletions
diff --git a/src/common-lib/logging.cpp b/src/common-lib/logging.cpp
index 55f4077f..a8138426 100644
--- a/src/common-lib/logging.cpp
+++ b/src/common-lib/logging.cpp
@@ -174,6 +174,9 @@ struct LoggingGlobal
QMutex deferredMessagesMutex;
std::vector<DeferredMessage> deferredMessages;
+ // Try to re-use a 512 byte buffer per thread as much as possible to avoid allocations.
+ QThreadStorage<QByteArray> outBuffers;
+
~LoggingGlobal();
};
@@ -215,9 +218,7 @@ static void colorLogToStderr(QtMsgType msgType, const QMessageLogContext &contex
if (msgType < QtDebugMsg || msgType > QtInfoMsg)
msgType = QtCriticalMsg;
- // Try to re-use a 512 byte buffer per thread as much as possible to avoid allocations.
- static QThreadStorage<QByteArray> outBuffers;
- QByteArray &out = outBuffers.localData();
+ QByteArray &out = lg()->outBuffers.localData();
if (out.capacity() > 512)
out.clear();
if (!out.capacity())
@@ -395,6 +396,10 @@ LoggingGlobal::~LoggingGlobal()
// we do an unnecessary (but cheap) qInstallMessageHandler() at program exit, but this way
// we cannot forget to dump the deferred messages whenever we exit
Logging::completeSetup();
+
+ // we are dead now, so make sure that anyone logging after this point will not crash the program
+ if (defaultQtHandler)
+ qInstallMessageHandler(defaultQtHandler);
}
QStringList Logging::filterRules()