src/hbcore/image/hbiconloader.cpp
changeset 30 80e4d18b72f5
parent 28 b7da29130b0e
--- a/src/hbcore/image/hbiconloader.cpp	Fri Sep 17 08:32:10 2010 +0300
+++ b/src/hbcore/image/hbiconloader.cpp	Mon Oct 04 00:38:12 2010 +0300
@@ -84,7 +84,7 @@
 */
 
 // Allocated dynamically so it can be deleted before the application object is destroyed.
-// Deleting it later causes segmentation fault.
+// Deleting it later causes segmentation fault so Q_GLOBAL_STATIC cannot be used.
 static HbIconLoader *theLoader = 0;
 static bool loaderDestroyed = false;
 
@@ -96,6 +96,14 @@
 // Icons with size above a certain limit are always ignored by the cachekeeper.
 const int MAX_KEEPALIVE_ITEM_SIZE_BYTES = MAX_KEEPALIVE_CACHE_SIZE_BYTES / 2;
 
+static void cleanupLoader()
+{
+    if (theLoader) {
+        delete theLoader;
+        theLoader = 0;
+    }
+}
+    
 class HbLocalLoaderThread : public QThread
 {
 public:
@@ -620,8 +628,13 @@
     // Set default rendering mode to EHWRendering
     renderMode = EHWRendering;
 
-    // Delete the icon loader when the application is destroyed.
-    connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), SLOT(destroy()));
+    // Delete the icon loader during QCoreApplication destruction. Relying on
+    // aboutToQuit() would cause destruction to happen too early, with Q_GLOBAL_STATIC it
+    // would be too late.  Recreation is forbidden so HbIconLoader::global() will return 0
+    // after destroying the application instance but at least in a typical application the
+    // loader will still be available e.g. in widget (and thus graphics widget, hb view,
+    // etc.) destructors.
+    qAddPostRoutine(cleanupLoader);
 
     connect(HbLayoutDirectionNotifier::instance(), SIGNAL(layoutDirectionChangeStarted()),
             this, SLOT(updateLayoutDirection()));
@@ -649,7 +662,9 @@
     if (!theLoader && !loaderDestroyed) {
         theLoader = new HbIconLoader;
     }
-
+    if (!theLoader) {
+        qWarning("HbIconLoader instance not present, returning null.");
+    }
     return theLoader;
 }
 
@@ -829,10 +844,7 @@
 
 void HbIconLoader::destroy()
 {
-    if (theLoader) {
-        delete theLoader;
-        theLoader = 0;
-    }
+    cleanupLoader();
 }
 
 void HbIconLoader::updateLayoutDirection()