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 <paul.tvete@digia.com>
This commit is contained in:
Eskil Abrahamsen Blomfeldt 2014-03-12 12:48:19 +01:00 committed by The Qt Project
parent de51854726
commit 287fa94fe2
2 changed files with 12 additions and 4 deletions

View File

@ -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<AndroidAssetDir> *aad = m_assetsCache.object(path);
m_assetsCacheMutext.unlock();
if (!aad) {

View File

@ -58,12 +58,13 @@ public:
QAbstractFileEngine *create(const QString &fileName) const;
private:
void prepopulateCache();
void prepopulateCache() const;
AAssetManager *m_assetManager;
mutable QCache<QByteArray, QSharedPointer<AndroidAssetDir>> m_assetsCache;
mutable QMutex m_assetsCacheMutext;
bool m_hasPrepopulatedCache;
mutable bool m_hasPrepopulatedCache;
mutable bool m_hasTriedPrepopulatingCache;
};
#endif // QANDROIDASSETSFILEENGINEHANDLER_H