diff -Naur text-orig/qfont.cpp text/qfont.cpp
--- text-orig/qfont.cpp	2013-10-01 16:25:01.573380487 +1300
+++ text/qfont.cpp	2013-10-01 16:24:56.123774356 +1300
@@ -256,8 +256,8 @@
 
 QFontPrivate::~QFontPrivate()
 {
-    if (engineData)
-        engineData->ref.deref();
+    if (engineData && !engineData->ref.deref())
+        delete engineData;
     engineData = 0;
     if (scFont && scFont != this)
         scFont->ref.deref();
@@ -279,7 +279,8 @@
         script = QUnicodeTables::Common;
     if (engineData && engineData->fontCache != QFontCache::instance()) {
         // throw out engineData that came from a different thread
-        engineData->ref.deref();
+        if(!engineData->ref.deref())
+            delete engineData;
         engineData = 0;
     }
     if (!engineData || !QT_FONT_ENGINE_FROM_DATA(engineData, script))
@@ -398,13 +399,13 @@
 {
 #if !defined(Q_WS_MAC)
     for (int i = 0; i < QUnicodeTables::ScriptCount; ++i) {
-        if (engines[i])
-            engines[i]->ref.deref();
+        if (engines[i] && !engines[i]->ref.deref())
+            delete engines[i];
         engines[i] = 0;
     }
 #else
-    if (engine)
-        engine->ref.deref();
+    if (engine && !engine->ref.deref())
+        delete engine;
     engine = 0;
 #endif // Q_WS_X11 || Q_WS_WIN || Q_WS_MAC
 }
@@ -751,8 +752,8 @@
 void QFont::detach()
 {
     if (d->ref == 1) {
-        if (d->engineData)
-            d->engineData->ref.deref();
+        if (d->engineData && !d->engineData->ref.deref())
+            delete d->engineData;
         d->engineData = 0;
         if (d->scFont && d->scFont != d.data())
             d->scFont->ref.deref();
@@ -2798,7 +2799,7 @@
         EngineDataCache::ConstIterator it = engineDataCache.constBegin(),
                                  end = engineDataCache.constEnd();
         while (it != end) {
-            if (it.value()->ref == 0)
+            if (it.value()->ref.deref() == 0)
                 delete it.value();
             else
                 FC_DEBUG("QFontCache::~QFontCache: engineData %p still has refcount %d",
@@ -2806,24 +2807,6 @@
             ++it;
         }
     }
-    EngineCache::ConstIterator it = engineCache.constBegin(),
-                         end = engineCache.constEnd();
-    while (it != end) {
-        if (--it.value().data->cache_count == 0) {
-            if (it.value().data->ref == 0) {
-                FC_DEBUG("QFontCache::~QFontCache: deleting engine %p key=(%d / %g %g %d %d %d)",
-                         it.value().data, it.key().script, it.key().def.pointSize,
-                         it.key().def.pixelSize, it.key().def.weight, it.key().def.style,
-                         it.key().def.fixedPitch);
-
-                delete it.value().data;
-            } else {
-                FC_DEBUG("QFontCache::~QFontCache: engine = %p still has refcount %d",
-                         it.value().data, int(it.value().data->ref));
-            }
-        }
-        ++it;
-    }
 }
 
 void QFontCache::clear()
@@ -2835,16 +2818,14 @@
             QFontEngineData *data = it.value();
 #if !defined(Q_WS_MAC)
             for (int i = 0; i < QUnicodeTables::ScriptCount; ++i) {
-                if (data->engines[i]) {
-                    data->engines[i]->ref.deref();
-                    data->engines[i] = 0;
-                }
+                if (data->engines[i] && !data->engines[i]->ref.deref())
+                    delete data->engines[i];
+                data->engines[i] = 0;
             }
 #else
-            if (data->engine) {
-                data->engine->ref.deref();
-                data->engine = 0;
-            }
+            if (data->engine && !data->engine->ref.deref())
+                delete data->engine;
+            data->engine = 0;
 #endif
             ++it;
         }
@@ -2852,15 +2833,7 @@
 
     for (EngineCache::Iterator it = engineCache.begin(), end = engineCache.end();
          it != end; ++it) {
-        if (it->data->ref == 0) {
-            delete it->data;
-            it->data = 0;
-        }
-    }
-
-    for (EngineCache::Iterator it = engineCache.begin(), end = engineCache.end();
-         it != end; ++it) {
-        if (it->data && it->data->ref == 0) {
+        if (it->data->ref.deref() == 0) {
             delete it->data;
             it->data = 0;
         }
@@ -2895,6 +2868,13 @@
 {
     FC_DEBUG("QFontCache: inserting new engine data %p", engineData);
 
+    QFontEngineData *oldEngineData = engineDataCache.value(key);
+    if( engineData != oldEngineData ) {
+        engineData->ref.ref(); // the cache has a reference
+        if(oldEngineData && !oldEngineData->ref.deref())
+            delete oldEngineData;
+    }
+
     engineDataCache.insert(key, engineData);
     increaseCost(sizeof(QFontEngineData));
 }
@@ -2925,6 +2905,13 @@
     Engine data(engine);
     data.timestamp = ++current_timestamp;
 
+    QFontEngine *oldEngine = engineCache.value(key).data;
+    if( engine != oldEngine ) {
+        engine->ref.ref(); // the cache has a reference
+        if(oldEngine && !oldEngine->ref.deref())
+            delete oldEngine;
+    }
+    
     engineCache.insert(key, data);
 
     // only increase the cost if this is the first time we insert the engine
@@ -2984,12 +2971,11 @@
                 continue;
             }
 
-            if(it.value()->ref != 0) {
+            if(it.value()->ref > 1) { // TODO this doesn't make sense to me (why delete the engines[] here?)
                 for(int i = 0; i < QUnicodeTables::ScriptCount; ++i) {
-                    if(it.value()->engines[i]) {
-                        it.value()->engines[i]->ref.deref();
-                        it.value()->engines[i] = 0;
-                    }
+                    if(it.value()->engines[i] && !it.value()->engines[i]->ref.deref())
+                        delete it->value()->engines[i];
+                    it.value()->engines[i] = 0;
                 }
                 ++it;
             } else {
@@ -3000,7 +2986,8 @@
 
                 FC_DEBUG("    %p", rem.value());
 
-                delete rem.value();
+                if(!rem->ref.deref())
+                    delete rem.value();
                 engineDataCache.erase(rem);
             }
         }
@@ -3009,7 +2996,7 @@
     EngineCache::Iterator it = engineCache.begin(),
                          end = engineCache.end();
     while(it != end) {
-        if (it.value().data->ref != 0 || it.key().screen == 0) {
+        if (it.value().data->ref != 1 || it.key().screen == 0) {
             ++it;
             continue;
         }
@@ -3023,7 +3010,8 @@
             FC_DEBUG("    DELETE: last occurrence in cache");
 
             decreaseCost(it.value().data->cache_cost);
-            delete it.value().data;
+            if(!it.value().data->ref.deref())
+                delete it.value().data;
         }
 
         engineCache.erase(it++);
@@ -3072,7 +3060,7 @@
 #  endif // Q_WS_X11 || Q_WS_WIN
 #endif // QFONTCACHE_DEBUG
 
-            if (it.value()->ref != 0)
+            if (it.value()->ref > 1)
                 in_use_cost += engine_data_cost;
         }
     }
@@ -3088,7 +3076,7 @@
                      int(it.value().data->ref), it.value().data->cache_count,
                      it.value().data->cache_cost);
 
-            if (it.value().data->ref != 0)
+            if (it.value().data->ref > 1)
                 in_use_cost += it.value().data->cache_cost / it.value().data->cache_count;
         }
 
@@ -3138,7 +3126,7 @@
         EngineDataCache::Iterator it = engineDataCache.begin(),
                                  end = engineDataCache.end();
         while (it != end) {
-            if (it.value()->ref != 0) {
+            if (it.value()->ref > 1) {
                 ++it;
                 continue;
             }
@@ -3166,7 +3154,7 @@
         uint least_popular = ~0u;
 
         for (; it != end; ++it) {
-            if (it.value().data->ref != 0)
+            if (it.value().data->ref > 1)
                 continue;
 
             if (it.value().timestamp < oldest &&
@@ -3179,7 +3167,7 @@
         FC_DEBUG("    oldest %u least popular %u", oldest, least_popular);
 
         for (it = engineCache.begin(); it != end; ++it) {
-            if (it.value().data->ref == 0 &&
+            if (it.value().data->ref == 1 &&
                  it.value().timestamp == oldest &&
                  it.value().hits == least_popular)
                 break;
@@ -3195,7 +3183,8 @@
                 FC_DEBUG("    DELETE: last occurrence in cache");
 
                 decreaseCost(it.value().data->cache_cost);
-                delete it.value().data;
+                if(!it.value().data->ref.deref())
+                    delete it.value().data;
             } else {
                 /*
                   this particular font engine is in the cache multiple
diff -Naur text-orig/qfontengine.cpp text/qfontengine.cpp
--- text-orig/qfontengine.cpp	2013-10-01 16:25:01.573380487 +1300
+++ text/qfontengine.cpp	2013-10-01 16:24:56.133773629 +1300
@@ -1306,11 +1306,8 @@
 {
     for (int i = 0; i < engines.size(); ++i) {
         QFontEngine *fontEngine = engines.at(i);
-        if (fontEngine) {
-            fontEngine->ref.deref();
-            if (fontEngine->cache_count == 0 && fontEngine->ref == 0)
-                delete fontEngine;
-        }
+        if (fontEngine && !fontEngine->ref.deref())
+            delete fontEngine;
     }
 }
 
diff -Naur text-orig/qrawfont.cpp text/qrawfont.cpp
--- text-orig/qrawfont.cpp	2013-10-01 16:25:01.593379053 +1300
+++ text/qrawfont.cpp	2013-10-01 16:24:56.143772898 +1300
@@ -663,8 +663,7 @@
     if (d->fontEngine != 0)
         d->fontEngine->ref.ref();
 
-    oldFontEngine->ref.deref();
-    if (oldFontEngine->cache_count == 0 && oldFontEngine->ref == 0)
+    if(!oldFontEngine->ref.deref())
         delete oldFontEngine;
 }
 
@@ -674,12 +673,10 @@
 void QRawFontPrivate::cleanUp()
 {
     platformCleanUp();
-    if (fontEngine != 0) {
-        fontEngine->ref.deref();
-        if (fontEngine->cache_count == 0 && fontEngine->ref == 0)
-            delete fontEngine;
-        fontEngine = 0;
-    }
+    if (fontEngine != 0 && !fontEngine->ref.deref())
+        delete fontEngine;
+    fontEngine = 0;
+
     hintingPreference = QFont::PreferDefaultHinting;
 }
 
diff -Naur text-orig/qrawfont_win.cpp text/qrawfont_win.cpp
--- text-orig/qrawfont_win.cpp	2013-10-01 16:25:01.593379053 +1300
+++ text/qrawfont_win.cpp	2013-10-01 16:24:56.143772898 +1300
@@ -581,11 +581,11 @@
             if (request.family != fontEngine->fontDef.family) {
                 qWarning("QRawFont::platformLoadFromData: Failed to load font. "
                          "Got fallback instead: %s", qPrintable(fontEngine->fontDef.family));
-                if (fontEngine->cache_count == 0 && fontEngine->ref == 0)
+                if (fontEngine->ref == 0)
                     delete fontEngine;
                 fontEngine = 0;
             } else {
-                Q_ASSERT(fontEngine->cache_count == 0 && fontEngine->ref == 0);
+                Q_ASSERT(fontEngine->ref == 0);
 
                 // Override the generated font name
                 static_cast<QFontEngineWin *>(fontEngine)->uniqueFamilyName = uniqueFamilyName;
diff -Naur text-orig/qstatictext.cpp text/qstatictext.cpp
--- text-orig/qstatictext.cpp	2013-10-01 16:25:01.593379053 +1300
+++ text/qstatictext.cpp	2013-10-01 16:24:56.143772898 +1300
@@ -705,10 +705,8 @@
 
 void QStaticTextItem::setFontEngine(QFontEngine *fe)
 {
-    if (m_fontEngine != 0) {
-        if (!m_fontEngine->ref.deref())
-            delete m_fontEngine;
-    }
+    if (m_fontEngine != 0 && !m_fontEngine->ref.deref())
+        delete m_fontEngine;
 
     m_fontEngine = fe;
     if (m_fontEngine != 0)
diff -Naur text-orig/qtextengine.cpp text/qtextengine.cpp
--- text-orig/qtextengine.cpp	2013-10-01 16:25:01.603378326 +1300
+++ text/qtextengine.cpp	2013-10-01 16:24:56.163771464 +1300
@@ -1434,11 +1434,8 @@
 
 static inline void releaseCachedFontEngine(QFontEngine *fontEngine)
 {
-    if (fontEngine) {
-        fontEngine->ref.deref();
-        if (fontEngine->cache_count == 0 && fontEngine->ref == 0)
-            delete fontEngine;
-    }
+    if (fontEngine && !fontEngine->ref.deref())
+        delete fontEngine;
 }
 
 void QTextEngine::resetFontEngineCache()
