From 287fa94fe2f93e2857a4c15f69435c4ea14de82e Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Wed, 12 Mar 2014 12:48:19 +0100 Subject: [PATCH] Android: Fix another "QApplication not on main() thread" crash The assets file engine is created before main() is called, thus prepopulate was called before main(). In this function, we created a QDataStream which internally created a QBuffer which inherits from QObject. This caused the main thread pointer to be initialized early, and the old "QApplication is not on the main() thread" warning and crash returned. The fix is to prepopulate the first time the file engine is used instead. Task-number: QTBUG-37444 Change-Id: I2c6e5f1a8ca88b5dc7d8e145fffeb7587dc0e623 Reviewed-by: Paul Olav Tvete --- .../android/qandroidassetsfileenginehandler.cpp | 11 +++++++++-- .../android/qandroidassetsfileenginehandler.h | 5 +++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp b/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp index b112e265a54..4968b8f188a 100644 --- a/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp +++ b/src/plugins/platforms/android/qandroidassetsfileenginehandler.cpp @@ -262,17 +262,20 @@ private: AndroidAssetsFileEngineHandler::AndroidAssetsFileEngineHandler() : m_assetsCache(std::max(5, qgetenv("QT_ANDROID_MAX_ASSETS_CACHE_SIZE").toInt())) , m_hasPrepopulatedCache(false) + , m_hasTriedPrepopulatingCache(false) { m_assetManager = QtAndroid::assetManager(); - prepopulateCache(); } AndroidAssetsFileEngineHandler::~AndroidAssetsFileEngineHandler() { } -void AndroidAssetsFileEngineHandler::prepopulateCache() +void AndroidAssetsFileEngineHandler::prepopulateCache() const { + Q_ASSERT(!m_hasTriedPrepopulatingCache); + m_hasTriedPrepopulatingCache = true; + QMutexLocker locker(&m_assetsCacheMutext); Q_ASSERT(m_assetsCache.isEmpty()); @@ -364,7 +367,11 @@ QAbstractFileEngine * AndroidAssetsFileEngineHandler::create(const QString &file if (!path.size()) path = fileName.left(fileName.length() - 1).toUtf8(); + m_assetsCacheMutext.lock(); + if (!m_hasTriedPrepopulatingCache) + prepopulateCache(); + QSharedPointer *aad = m_assetsCache.object(path); m_assetsCacheMutext.unlock(); if (!aad) { diff --git a/src/plugins/platforms/android/qandroidassetsfileenginehandler.h b/src/plugins/platforms/android/qandroidassetsfileenginehandler.h index d56367d4d80..ac16ad7b790 100644 --- a/src/plugins/platforms/android/qandroidassetsfileenginehandler.h +++ b/src/plugins/platforms/android/qandroidassetsfileenginehandler.h @@ -58,12 +58,13 @@ public: QAbstractFileEngine *create(const QString &fileName) const; private: - void prepopulateCache(); + void prepopulateCache() const; AAssetManager *m_assetManager; mutable QCache> m_assetsCache; mutable QMutex m_assetsCacheMutext; - bool m_hasPrepopulatedCache; + mutable bool m_hasPrepopulatedCache; + mutable bool m_hasTriedPrepopulatingCache; }; #endif // QANDROIDASSETSFILEENGINEHANDLER_H