Bug 1054819 - Ensure that the dictionary is released on the main thread. r=ehsan, a=sledru
authorMichal Novotny <michal.novotny>
Wed, 20 Aug 2014 04:28:00 -0400
changeset 208355 c06efff91ed3
parent 208354 fa7360fe9779
child 208356 f5d4b16203aa
push id3836
push userryanvm@gmail.com
push date2014-08-20 23:01 +0000
treeherdermozilla-beta@f5d4b16203aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan, sledru
bugs1054819
milestone32.0
Bug 1054819 - Ensure that the dictionary is released on the main thread. r=ehsan, a=sledru
extensions/spellcheck/src/mozPersonalDictionary.cpp
--- a/extensions/spellcheck/src/mozPersonalDictionary.cpp
+++ b/extensions/spellcheck/src/mozPersonalDictionary.cpp
@@ -13,16 +13,17 @@
 #include "nsIPrefBranch.h"
 #include "nsIWeakReference.h"
 #include "nsCRT.h"
 #include "nsNetUtil.h"
 #include "nsStringEnumerator.h"
 #include "nsUnicharInputStream.h"
 #include "nsIRunnable.h"
 #include "nsThreadUtils.h"
+#include "nsProxyRelease.h"
 
 #define MOZ_PERSONAL_DICT_NAME "persdict.dat"
 
 const int kMaxWordLen=256;
 
 /**
  * This is the most braindead implementation of a personal dictionary possible.
  * There is not much complexity needed, though.  It could be made much faster,
@@ -52,21 +53,28 @@ class mozPersonalDictionaryLoader MOZ_FI
 {
 public:
   mozPersonalDictionaryLoader(mozPersonalDictionary *dict) : mDict(dict)
   {
   }
 
   NS_IMETHOD Run()
   {
-    if (!NS_IsMainThread()) {
-      mDict->SyncLoad();
+    mDict->SyncLoad();
+
+    // Release the dictionary on the main thread
+    mozPersonalDictionary *dict;
+    mDict.forget(&dict);
 
-      // Release refptr on the mainthread
-      NS_DispatchToMainThread(this);
+    nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
+    if (mainThread) {
+      NS_ProxyRelease(mainThread, static_cast<mozIPersonalDictionary *>(dict));
+    } else {
+      // It's better to leak the dictionary than to release it on a wrong thread
+      NS_WARNING("Cannot get main thread, leaking mozPersonalDictionary.");
     }
 
     return NS_OK;
   }
 
 private:
   nsRefPtr<mozPersonalDictionary> mDict;
 };