Bug 735778 - Call MOZ_ASSERT if RemoveObserver is called with an observer that is not present. r=jlebar
authorMounir Lamouri <mounir.lamouri@gmail.com>
Fri, 16 Mar 2012 19:32:11 +0100
changeset 89666 0d61a0d8dba4278be6b363177f56a255ed1f6305
parent 89665 329c07609ca8e7558cba6bdac0b34df686b96312
child 89667 3a031783a2ea52db33856e41c6c81d35672a497e
push id22269
push userphilringnalda@gmail.com
push dateSat, 17 Mar 2012 23:57:42 +0000
treeherdermozilla-central@8910001d4347 [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,26 @@ 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) {
-      return;
-    }
+    // 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.
 
-    mObservers->RemoveObserver(aObserver);
+    MOZ_ASSERT(mObservers);
+
+    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();