Bug 1398357: Part 1 - Fix removal of LocaleService preference observers. r=gandalf
authorKris Maglione <maglione.k@gmail.com>
Fri, 08 Sep 2017 17:51:43 -0700
changeset 429379 4c33f18d2e96220b721d4a6c593e39939a13a99a
parent 429378 f5c3628ecd2f2a45e1d54930c069c809aef33c23
child 429380 d59f7e89c0884cb430520b6bb136cc880fd3375d
push id7761
push userjlund@mozilla.com
push dateFri, 15 Sep 2017 00:19:52 +0000
treeherdermozilla-beta@c38455951db4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgandalf
bugs1398357
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
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
@@ -31,17 +31,18 @@ static const char* kObservedPrefs[] = {
   SELECTED_LOCALE_PREF,
   ANDROID_OS_LOCALE_PREF,
   nullptr
 };
 
 using namespace mozilla::intl;
 using namespace mozilla;
 
-NS_IMPL_ISUPPORTS(LocaleService, mozILocaleService, nsIObserver)
+NS_IMPL_ISUPPORTS(LocaleService, mozILocaleService, nsIObserver,
+                  nsISupportsWeakReference)
 
 mozilla::StaticRefPtr<LocaleService> LocaleService::sInstance;
 
 /**
  * This function transforms a canonical Mozilla Language Tag, into it's
  * BCP47 compilant form.
  *
  * Example: "ja-JP-mac" -> "ja-JP-x-lvariant-mac"
@@ -177,28 +178,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.