|author||Tor Arne Vestbø <email@example.com>||2018-02-01 12:17:48 +0100|
|committer||Tor Arne Vestbø <firstname.lastname@example.org>||2018-02-07 11:19:44 +0000|
Android: Defer initialization of logging rules until qApp construction
On Android, we load the application library, and its dependencies (Qt), on Android's main thread (thread 0), and then spin up a secondary thread (thread 1), that we call main() on. If any QObject is constructed during loading of the application library or any of Qt's libraries, via static initializers or constructor functions, we will set QCoreApplicationPrivate::theMainThread to thread 0, which will confuse Qt later on when it's being run on thread 1, and will result in a warning during QCoreApplication construction: QApplication was not created in the main() thread This situation can easily lead to a crash as well. Unfortunately logging via qDebug/qCDebug and friends will trigger this too, as they internally use QObject. Fixing the root cause of this is under investigation, but for now we will partially revert fa2a653b3b934783 for Android. The effect is that any qCDebug with a "qt.*" category before qApp construction will turn into a no-op, like it was before fa2a653b3b934783. This patch does not cover the case of a regular qDebug, or a qCDebug with a non-Qt category. Those will still produce the same symptom, as before fa2a653b3b934783. Task-number: QTBUG-65863 Change-Id: I95675731d233244530d0a2a1c82a9578d5599775 Reviewed-by: Eskil Abrahamsen Blomfeldt <email@example.com> Reviewed-by: Tor Arne Vestbø <firstname.lastname@example.org> (cherry picked from commit 538b1b50764fb3a1898d425a7155319afbcf3b25)
2 files changed, 18 insertions, 0 deletions
diff --git a/src/corelib/io/qloggingregistry.cpp b/src/corelib/io/qloggingregistry.cpp
index b5f8e30b80..cd97268d71 100644
@@ -44,6 +44,7 @@
// We can't use the default macros because this would lead to recursion.
// Instead let's define our own one that unconditionally logs...
@@ -255,6 +256,15 @@ void QLoggingSettingsParser::parseNextLine(QStringRef line)
+ // Unless QCoreApplication has been constructed we can't be sure that
+ // we are on Qt's main thread. If we did allow logging here, we would
+ // potentially set Qt's main thread to Android's thread 0, which would
+ // confuse Qt later when running main().
+ if (!qApp)
initializeRules(); // Init on first use
diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp
index 12c2669f36..c8b130bbb9 100644
@@ -776,6 +776,14 @@ void QCoreApplicationPrivate::init()
coreappdata()->applicationVersion = appVersion();
+ // We've deferred initializing the logging registry due to not being
+ // able to guarantee that logging happened on the same thread as the
+ // Qt main thread, but now that the Qt main thread is set up, we can
+ // enable categorized logging.
// Reset the lib paths, so that they will be recomputed, taking the availability of argv
// into account. If necessary, recompute right away and replay the manual changes on top of the