Bug 1428184 - copy mStreams before iterating the array. draft
authorJW Wang <jwwang@mozilla.com>
Fri, 05 Jan 2018 11:34:59 +0800
changeset 716077 324a1fcc8f354c79094ab801835c0a41a30bfe7b
parent 716070 1c93558c5edb5ea0d2ba0c985f96a3115535aa7b
child 744941 ef7b0cae60a91468cd68cadf3c067a972f51acc2
push id94320
push userjwwang@mozilla.com
push dateFri, 05 Jan 2018 03:39:15 +0000
bugs1428184
milestone59.0a1
Bug 1428184 - copy mStreams before iterating the array. It is bad to modify the array while iterating it. MozReview-Commit-ID: JbpoRmM7GqP
dom/media/MediaCache.cpp
--- a/dom/media/MediaCache.cpp
+++ b/dom/media/MediaCache.cpp
@@ -722,26 +722,28 @@ MediaCache::Flush()
     });
   sThread->Dispatch(r.forget());
 }
 
 void
 MediaCache::CloseStreamsForPrivateBrowsing()
 {
   MOZ_ASSERT(NS_IsMainThread());
-  sThread->Dispatch(
-    NS_NewRunnableFunction("MediaCache::CloseStreamsForPrivateBrowsing",
-                           [self = RefPtr<MediaCache>(this)]() {
-                             AutoLock lock(self->mMonitor);
-                             for (MediaCacheStream* s : self->mStreams) {
-                               if (s->mIsPrivateBrowsing) {
-                                 s->CloseInternal(lock);
-                               }
-                             }
-                           }));
+  sThread->Dispatch(NS_NewRunnableFunction(
+    "MediaCache::CloseStreamsForPrivateBrowsing",
+    [self = RefPtr<MediaCache>(this)]() {
+      AutoLock lock(self->mMonitor);
+      // Copy mStreams since CloseInternal() will change the array.
+      nsTArray<MediaCacheStream*> streams(self->mStreams);
+      for (MediaCacheStream* s : streams) {
+        if (s->mIsPrivateBrowsing) {
+          s->CloseInternal(lock);
+        }
+      }
+    }));
 }
 
 /* static */ RefPtr<MediaCache>
 MediaCache::GetMediaCache(int64_t aContentLength)
 {
   NS_ASSERTION(NS_IsMainThread(), "Only call on main thread");
 
   if (!sThreadInit) {