Bug 743786 - Add IsMainThread assertions to ClearOnShutdown. r=bmsedberg
authorJustin Lebar <justin.lebar@gmail.com>
Tue, 10 Apr 2012 15:58:10 -0400
changeset 94665 0fd099d86cc1398e9f4cf9b88ea5b29e44cb0c54
parent 94664 91bbeb1c518702477e5a456474fe8ef5991f4a91
child 94666 d32163dc99028a006b28c09c333d6f5a0e241c12
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbmsedberg
bugs743786
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 743786 - Add IsMainThread assertions to ClearOnShutdown. r=bmsedberg
xpcom/base/ClearOnShutdown.h
--- a/xpcom/base/ClearOnShutdown.h
+++ b/xpcom/base/ClearOnShutdown.h
@@ -36,31 +36,36 @@
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #ifndef mozilla_ClearOnShutdown_h
 #define mozilla_ClearOnShutdown_h
 
 #include "mozilla/LinkedList.h"
+#include "nsThreadUtils.h"
 
 /*
  * This header exports one public method in the mozilla namespace:
  *
  *   template<class SmartPtr>
  *   void ClearOnShutdown(SmartPtr *aPtr)
  *
  * This function takes a pointer to a smart pointer (i.e., nsCOMPtr<T>*,
  * nsRefPtr<T>*, or nsAutoPtr<T>*) and nulls the smart pointer on shutdown.
  *
  * This is useful if you have a global smart pointer object which you don't
  * want to "leak" on shutdown.
  *
  * There is no way to undo a call to ClearOnShutdown, so you can call it only
  * on smart pointers which you know will live until the program shuts down.
+ *
+ * ClearOnShutdown is currently main-thread only because we don't want to
+ * accidentally free an object from a different thread than the one it was
+ * created on.
  */
 
 namespace mozilla {
 namespace ClearOnShutdown_Internal {
 
 class ShutdownObserver : public LinkedListElement<ShutdownObserver>
 {
 public:
@@ -91,27 +96,31 @@ extern LinkedList<ShutdownObserver> sShu
 
 } // namespace ClearOnShutdown_Internal
 
 template<class SmartPtr>
 inline void ClearOnShutdown(SmartPtr *aPtr)
 {
   using namespace ClearOnShutdown_Internal;
 
+  MOZ_ASSERT(NS_IsMainThread());
+
   MOZ_ASSERT(!sHasShutDown);
   ShutdownObserver *observer = new PointerClearer<SmartPtr>(aPtr);
   sShutdownObservers.insertBack(observer);
 }
 
 // Called when XPCOM is shutting down, after all shutdown notifications have
 // been sent and after all threads' event loops have been purged.
 inline void KillClearOnShutdown()
 {
   using namespace ClearOnShutdown_Internal;
 
+  MOZ_ASSERT(NS_IsMainThread());
+
   ShutdownObserver *observer;
   while ((observer = sShutdownObservers.popFirst())) {
     observer->Shutdown();
     delete observer;
   }
 
   sHasShutDown = true;
 }