From 52ba7b1ffcb90772dc97b3e9a34beee5781ed2d7 Mon Sep 17 00:00:00 2001 From: Robin Burchell Date: Wed, 13 Nov 2013 14:54:49 +0100 Subject: Cleanup freetype data in a thread-safe way One less obvious part of this patch: the fontCache pointer in engineData was not safe. It isn't safe to rely on pointer addresses to verify we're cleaning up the right thing, as a sequence of malloc()/free()/malloc() can return the same pointer, and nothing was cleaning up the dangling pointer in engineData. With this, it is possible to safely drop OpenGL contexts in QtQuick under all conditions with no possibility of crashes. Done-with: Aaron Kennedy Change-Id: I7b91384251593730124323a74737d41333a05f59 Reviewed-by: Konstantin Ritt Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/gui/text/qfont.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'src/gui/text/qfont.cpp') diff --git a/src/gui/text/qfont.cpp b/src/gui/text/qfont.cpp index a410004c06..49b5a9ba46 100644 --- a/src/gui/text/qfont.cpp +++ b/src/gui/text/qfont.cpp @@ -208,7 +208,7 @@ QFontEngine *QFontPrivate::engineForScript(int script) const QMutexLocker locker(qt_fontdatabase_mutex()); if (script <= QChar::Script_Latin) script = QChar::Script_Common; - if (engineData && engineData->fontCache != QFontCache::instance()) { + if (engineData && engineData->fontCacheId != QFontCache::instance()->id()) { // throw out engineData that came from a different thread if (!engineData->ref.deref()) delete engineData; @@ -317,7 +317,7 @@ void QFontPrivate::resolve(uint mask, const QFontPrivate *other) QFontEngineData::QFontEngineData() - : ref(0), fontCache(QFontCache::instance()) + : ref(0), fontCacheId(QFontCache::instance()->id()) { memset(engines, 0, QChar::ScriptCount * sizeof(QFontEngine *)); } @@ -2638,9 +2638,12 @@ void QFontCache::cleanup() } #endif // QT_NO_THREAD +QBasicAtomicInt font_cache_id = Q_BASIC_ATOMIC_INITIALIZER(1); + QFontCache::QFontCache() : QObject(), total_cost(0), max_cost(min_cost), - current_timestamp(0), fast(false), timer_id(-1) + current_timestamp(0), fast(false), timer_id(-1), + m_id(font_cache_id.fetchAndAddRelaxed(1)) { } -- cgit v1.2.3