QML: Only release types if they aren't referenced anymore
authorUlf Hermann <ulf.hermann@qt.io>
Wed, 8 Jun 2016 15:32:32 +0000 (17:32 +0200)
committerLisandro Damián Nicanor Pérez Meyer <lisandro@debian.org>
Sun, 11 Sep 2016 12:30:31 +0000 (12:30 +0000)
Just checking for references on m_compiledData is not enough. The
actual component can also be referenced. Thus it won't be deleted
on release(), but cannot be found in the type cache anymore.

Task-number: QTBUG-53761
Change-Id: I8567af8e75a078598e4fed31e4717134e1332278
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
(cherry picked from commit 2ac19881f92c94f4e9427bd9ff513210675f259e)

Gbp-Pq: Name qml_only_release_types_if_they_arent_referenced_anymore.patch

src/qml/qml/qqmltypeloader.cpp
tests/auto/qml/qqmltypeloader/tst_qqmltypeloader.cpp

index c684c8602ee72a602cee86d2d9f1920a31812ee0..01200fd8816b09b46e0bead247d5e3f37f73ee5a 100644 (file)
@@ -1961,7 +1961,8 @@ void QQmlTypeLoader::trimCache()
         QList<TypeCache::Iterator> unneededTypes;
         for (TypeCache::Iterator iter = m_typeCache.begin(), end = m_typeCache.end(); iter != end; ++iter)  {
             QQmlTypeData *typeData = iter.value();
-            if (typeData->m_compiledData && typeData->m_compiledData->count() == 1) {
+            if (typeData->m_compiledData && typeData->count() == 1
+                    && typeData->m_compiledData->count() == 1) {
                 // There are no live objects of this type
                 unneededTypes.append(iter);
             }
index 7045c7cbd41d87442435c93b1165eca0ea191ded..a1eaa0567fd264d58bc6c7b137a36a3eaf0ae35f 100644 (file)
@@ -86,10 +86,19 @@ void tst_QQMLTypeLoader::trimCache()
         url.setQuery(QString::number(i));
 
         QQmlTypeData *data = loader.getType(url);
-        if (i % 5 == 0) // keep references to some of them so that they aren't trimmed
-            data->compiledData()->addref();
+        // Run an event loop to receive the callback that release()es.
+        QTRY_COMPARE(data->count(), 2);
 
-        data->release();
+        // keep references to some of them so that they aren't trimmed. References to either the
+        // QQmlTypeData or its compiledData() should prevent the trimming.
+        if (i % 10 == 0) {
+            // keep ref on data, don't add ref on data->compiledData()
+        } else if (i % 5 == 0) {
+            data->compiledData()->addref();
+            data->release();
+        } else {
+            data->release();
+        }
     }
 
     for (int i = 0; i < 256; ++i) {