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
--- 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.