Bug 1398357: Part 1 - Fix removal of LocaleService preference observers. r?gandalf draft
authorKris Maglione <maglione.k@gmail.com>
Fri, 08 Sep 2017 14:35:56 -0700
changeset 661698 26af7e522b9500fe7aa92bffe3201e2a3a8c7c16
parent 661692 2732fcf15636f23394c163b8e6d2ec3e29f400d1
child 661699 7ca0c100ce6f4f9aeb15456063ff17418a495628
push id78852
push usermaglione.k@gmail.com
push dateFri, 08 Sep 2017 21:37:36 +0000
reviewersgandalf
bugs1398357
milestone57.0a1
Bug 1398357: Part 1 - Fix removal of LocaleService preference observers. r?gandalf By the time the LocaleService destructor is called, by definition sInstance has been cleared and its preference observers have been removed, since both are strong references. In practice, this doesn't seem to cause trouble, but it does lead to worrying warnings at shutdown in debug builds, which are annoying red herrings when debugging other issues. MozReview-Commit-ID: IalOigr2GWN
intl/locale/LocaleService.cpp
intl/locale/LocaleService.h
--- a/intl/locale/LocaleService.cpp
+++ b/intl/locale/LocaleService.cpp
@@ -177,28 +177,28 @@ LocaleService*
 LocaleService::GetInstance()
 {
   if (!sInstance) {
     sInstance = new LocaleService(XRE_IsParentProcess());
 
     if (sInstance->IsServer()) {
       // We're going to observe for requested languages changes which come
       // from prefs.
-      DebugOnly<nsresult> rv = Preferences::AddStrongObservers(sInstance, kObservedPrefs);
+      DebugOnly<nsresult> rv = Preferences::AddWeakObservers(sInstance, kObservedPrefs);
       MOZ_ASSERT(NS_SUCCEEDED(rv), "Adding observers failed.");
     }
     ClearOnShutdown(&sInstance);
   }
   return sInstance;
 }
 
 LocaleService::~LocaleService()
 {
   if (mIsServer) {
-    Preferences::RemoveObservers(sInstance, kObservedPrefs);
+    Preferences::RemoveObservers(this, kObservedPrefs);
   }
 }
 
 void
 LocaleService::GetAppLocalesAsLangTags(nsTArray<nsCString>& aRetVal)
 {
   if (mAppLocales.IsEmpty()) {
     NegotiateAppLocales(mAppLocales);
--- a/intl/locale/LocaleService.h
+++ b/intl/locale/LocaleService.h
@@ -4,16 +4,17 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_intl_LocaleService_h__
 #define mozilla_intl_LocaleService_h__
 
 #include "nsIObserver.h"
 #include "nsString.h"
 #include "nsTArray.h"
+#include "nsWeakReference.h"
 
 #include "mozILocaleService.h"
 
 namespace mozilla {
 namespace intl {
 
 /**
  * LocaleService is a manager of language negotiation in Gecko.
@@ -63,17 +64,18 @@ namespace intl {
  *
  * An example of a Locale ID is `en-Latn-US-x-hc-h12-ca-gregory`
  *
  * At the moment we do not support full extension tag system, but we
  * try to be specific when naming APIs, so the service is for locales,
  * but we negotiate between languages etc.
  */
 class LocaleService : public mozILocaleService,
-                      public nsIObserver
+                      public nsIObserver,
+                      public nsSupportsWeakReference
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIOBSERVER
   NS_DECL_MOZILOCALESERVICE
 
   /**
    * List of available language negotiation strategies.