Bug 735778 - Call MOZ_ASSERT if RemoveObserver is called with an observer that is not present. r=jlebar
authorMounir Lamouri <mounir.lamouri@gmail.com>
Mon, 19 Mar 2012 16:49:39 +0100
changeset 89746 436f102861fc12ba11a58d91670e444f43d831e5
parent 89745 d5713db687498156de6728400be91a7889b4ae10
child 89747 2a46d0a938161aa88180349ca7693050d3d8cba9
push id22282
push usermlamouri@mozilla.com
push dateTue, 20 Mar 2012 10:40:32 +0000
treeherdermozilla-central@ee554888d071 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjlebar
bugs735778
milestone14.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 735778 - Call MOZ_ASSERT if RemoveObserver is called with an observer that is not present. r=jlebar
hal/Hal.cpp
xpcom/glue/Observer.h
--- a/hal/Hal.cpp
+++ b/hal/Hal.cpp
@@ -174,23 +174,32 @@ public:
     mObservers->AddObserver(aObserver);
 
     if (mObservers->Length() == 1) {
       EnableNotifications();
     }
   }
 
   void RemoveObserver(Observer<InfoType>* aObserver) {
-    // If mObservers is null, that means there are no observers, so removing one
-    // must be a no-op.
+    // If mObservers is null, that means there are no observers.
+    // In addition, if RemoveObserver() returns false, that means we didn't
+    // find the observer.
+    // In both cases, that is a logical error we want to make sure the developer
+    // notices.
+
+    MOZ_ASSERT(mObservers);
+
+#ifndef DEBUG
     if (!mObservers) {
       return;
     }
+#endif
 
-    mObservers->RemoveObserver(aObserver);
+    DebugOnly<bool> removed = mObservers->RemoveObserver(aObserver);
+    MOZ_ASSERT(removed);
 
     if (mObservers->Length() == 0) {
       DisableNotifications();
 
       OnNotificationsDisabled();
 
       delete mObservers;
       mObservers = 0;
--- a/xpcom/glue/Observer.h
+++ b/xpcom/glue/Observer.h
@@ -76,18 +76,22 @@ public:
    * called.
    *
    * @see RemoveObserver()
    */
   void AddObserver(Observer<T>* aObserver) {
     mObservers.AppendElement(aObserver);
   }
 
-  void RemoveObserver(Observer<T>* aObserver) {
-    mObservers.RemoveElement(aObserver);
+  /**
+   * Remove the observer from the observer list.
+   * @return Whether the observer has been found in the list.
+   */
+  bool RemoveObserver(Observer<T>* aObserver) {
+    return mObservers.RemoveElement(aObserver);
   }
 
   PRUint32 Length() {
     return mObservers.Length();
   }
 
   void Broadcast(const T& aParam) {
     PRUint32 size = mObservers.Length();