Bug 717498 - Clear nsThread::mEventObservers at thread shutdown. r=bsmedberg
authorBobby Holley <bobbyholley@gmail.com>
Sat, 14 Jan 2012 10:31:13 -0800
changeset 85726 3f65bc07bd89ce79411cde7f73cbcbb71f8acc89
parent 85725 3ab1dcfb2218efb5d24a582bb160aa10e7cce891
child 85727 a0e3fb36fb1acb91155aa0193263c1e142a69df4
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs717498
milestone12.0a1
Bug 717498 - Clear nsThread::mEventObservers at thread shutdown. r=bsmedberg
xpcom/threads/nsThread.cpp
xpcom/threads/nsThread.h
xpcom/threads/nsThreadManager.cpp
--- a/xpcom/threads/nsThread.cpp
+++ b/xpcom/threads/nsThread.cpp
@@ -501,16 +501,21 @@ nsThread::Shutdown()
   while (!context.shutdownAck)
     NS_ProcessNextEvent(context.joiningThread);
 
   // Now, it should be safe to join without fear of dead-locking.
 
   PR_JoinThread(mThread);
   mThread = nsnull;
 
+  // We hold strong references to our event observers, and once the thread is
+  // shut down the observers can't easily unregister themselves. Do it here
+  // to avoid leaking.
+  ClearObservers();
+
 #ifdef DEBUG
   {
     MutexAutoLock lock(mLock);
     NS_ASSERTION(!mObserver, "Should have been cleared at shutdown!");
   }
 #endif
 
   return NS_OK;
--- a/xpcom/threads/nsThread.h
+++ b/xpcom/threads/nsThread.h
@@ -72,16 +72,19 @@ public:
 
   // The PRThread corresponding to this thread.
   PRThread *GetPRThread() { return mThread; }
 
   // If this flag is true, then the nsThread was created using
   // nsIThreadManager::NewThread.
   bool ShutdownRequired() { return mShutdownRequired; }
 
+  // Clear the observer list.
+  void ClearObservers() { mEventObservers.Clear(); }
+
   // The global thread observer
   static nsIThreadObserver* sGlobalObserver;
 
 private:
   friend class nsThreadShutdownEvent;
 
   ~nsThread();
 
--- a/xpcom/threads/nsThreadManager.cpp
+++ b/xpcom/threads/nsThreadManager.cpp
@@ -171,16 +171,17 @@ nsThreadManager::Shutdown()
     MutexAutoLock lock(*mLock);
     mThreadsByPRThread.Clear();
   }
 
   // Normally thread shutdown clears the observer for the thread, but since the
   // main thread is special we do it manually here after we're sure all events
   // have been processed.
   mMainThread->SetObserver(nsnull);
+  mMainThread->ClearObservers();
 
   // Release main thread object.
   mMainThread = nsnull;
   mLock = nsnull;
 
   // Remove the TLS entry for the main thread.
   PR_SetThreadPrivate(mCurThreadIndex, nsnull);
 }