Bug 1412181 - add a proxy to be registered with ClearOnShutdown() to clear MediaCache::sThread. draft
authorJW Wang <jwwang@mozilla.com>
Fri, 27 Oct 2017 11:38:53 +0800
changeset 688390 c70c88dce29bc8cce4a5812242a320aff2520375
parent 688389 2a38d6e34afeb6aa5792fd14865deb72eea6a4d2
child 688392 927f660917377296f3e826df18e7086e58c1370f
push id86738
push userjwwang@mozilla.com
push dateMon, 30 Oct 2017 02:14:44 +0000
bugs1412181
milestone58.0a1
Bug 1412181 - add a proxy to be registered with ClearOnShutdown() to clear MediaCache::sThread. MozReview-Commit-ID: 9SXSKTP9Twb
dom/media/MediaCache.cpp
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -264,25 +264,16 @@ public:
       return nullptr;
     }
   private:
     MediaCache* mMediaCache;
     int64_t  mResourceID;
     uint32_t mNext;
   };
 
-  // Called during shutdown to clear sThread.
-  void operator=(std::nullptr_t)
-  {
-    nsCOMPtr<nsIThread> thread = sThread.forget();
-    if (thread) {
-      thread->Shutdown();
-    }
-  }
-
 protected:
   explicit MediaCache(MediaBlockCacheBase* aCache)
     : mNextResourceID(1)
     , mReentrantMonitor("MediaCache.mReentrantMonitor")
     , mBlockCache(aCache)
     , mUpdateQueued(false)
 #ifdef DEBUG
     , mInUpdate(false)
@@ -722,20 +713,28 @@ MediaCache::GetMediaCache(int64_t aConte
     sThreadInit = true;
     nsCOMPtr<nsIThread> thread;
     nsresult rv = NS_NewNamedThread("MediaCache", getter_AddRefs(thread));
     if (NS_FAILED(rv)) {
       NS_WARNING("Failed to create a thread for MediaCache.");
       return nullptr;
     }
     sThread = thread.forget();
-    // Note it is safe to pass an invalid pointer for operator=(std::nullptr_t)
-    // is non-virtual and it will not access |this|.
-    ClearOnShutdown(reinterpret_cast<MediaCache*>(0x1),
-                    ShutdownPhase::ShutdownThreads);
+
+    static struct ClearThread
+    {
+      // Called during shutdown to clear sThread.
+      void operator=(std::nullptr_t)
+      {
+        nsCOMPtr<nsIThread> thread = sThread.forget();
+        MOZ_ASSERT(thread);
+        thread->Shutdown();
+      }
+    } sClearThread;
+    ClearOnShutdown(&sClearThread, ShutdownPhase::ShutdownThreads);
   }
 
   if (!sThread) {
     return nullptr;
   }
 
   if (aContentLength > 0 &&
       aContentLength <= int64_t(MediaPrefs::MediaMemoryCacheMaxSize()) * 1024) {