Bug 1208371 - Guard LoadManagerSingleton with a WeakPtr. r=pkerr
authorAndreas Pehrson <pehrsons@gmail.com>
Tue, 05 Jan 2016 10:16:32 +0800
changeset 348481 08e7e6ddff876f802bfe33c0763793cdb40ba981
parent 348480 ce5a05155b602ef692239c79425362816f307b77
child 348482 4d753fefa7765aadf91d92e91daf1cd7c60dcbf7
push id14828
push userpehrsons@gmail.com
push dateThu, 07 Apr 2016 12:57:27 +0000
reviewerspkerr
bugs1208371
milestone48.0a1
Bug 1208371 - Guard LoadManagerSingleton with a WeakPtr. r=pkerr LoadManagerSingleton has a separate shutdown path (xpcom-shutdown) from its users (Audio/VideoConduit - garbage collected). These have appeared racy, so in some cases the singleton was destructed before the users had deregistered (e.g., when conduits destructed by SnowWhiteKiller). A WeakPtr can solve this. MozReview-Commit-ID: AVrpd3QqOGx
dom/media/systemservices/LoadManager.h
--- a/dom/media/systemservices/LoadManager.h
+++ b/dom/media/systemservices/LoadManager.h
@@ -2,38 +2,42 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef _LOADMANAGER_H_
 #define _LOADMANAGER_H_
 
 #include "LoadMonitor.h"
+#include "mozilla/WeakPtr.h"
 #include "mozilla/StaticPtr.h"
 #include "mozilla/TimeStamp.h"
 #include "mozilla/Services.h"
 #include "nsTArray.h"
 #include "nsIObserver.h"
 
 #include "webrtc/common_types.h"
 #include "webrtc/video_engine/include/vie_base.h"
 
 extern mozilla::LazyLogModule gLoadManagerLog;
 
 namespace mozilla {
 
 class LoadManagerSingleton : public LoadNotificationCallback,
                              public webrtc::CPULoadStateCallbackInvoker,
                              public webrtc::CpuOveruseObserver,
+                             public SupportsWeakPtr<LoadManagerSingleton>,
                              public nsIObserver
 
 {
 public:
     static LoadManagerSingleton* Get();
 
+    MOZ_DECLARE_WEAKREFERENCE_TYPENAME(LoadManagerSingleton)
+
     NS_DECL_THREADSAFE_ISUPPORTS
     NS_DECL_NSIOBSERVER
 
     // LoadNotificationCallback interface
     void LoadChanged(float aSystemLoad, float aProcessLoad) override;
     // CpuOveruseObserver interface
     // Called as soon as an overuse is detected.
     void OveruseDetected() override;
@@ -81,30 +85,38 @@ class LoadManager final : public webrtc:
 public:
     explicit LoadManager(LoadManagerSingleton* aManager)
         : mManager(aManager)
     {}
     ~LoadManager() {}
 
     void AddObserver(webrtc::CPULoadStateObserver * aObserver) override
     {
-        mManager->AddObserver(aObserver);
+        if (mManager) {
+          mManager->AddObserver(aObserver);
+        }
     }
     void RemoveObserver(webrtc::CPULoadStateObserver * aObserver) override
     {
-        mManager->RemoveObserver(aObserver);
+        if (mManager) {
+          mManager->RemoveObserver(aObserver);
+        }
     }
     void OveruseDetected() override
     {
-        mManager->OveruseDetected();
+        if (mManager) {
+          mManager->OveruseDetected();
+        }
     }
     void NormalUsage() override
     {
-        mManager->NormalUsage();
+        if (mManager) {
+          mManager->NormalUsage();
+        }
     }
 
 private:
-    LoadManagerSingleton* mManager;
+    WeakPtr<LoadManagerSingleton> mManager;
 };
 
 } //namespace
 
 #endif /* _LOADMANAGER_H_ */