Bug 1428184 - copy mStreams before iterating the array. r=bechen,gerald
authorJW Wang <jwwang@mozilla.com>
Fri, 05 Jan 2018 11:34:59 +0800
changeset 449648 1f394da377b0250c21506645b5535dc4a8e7cdf0
parent 449647 94114f8f6418c07bffba1744186f466514c447d9
child 449649 26c6c1e829af3e0d659d4a3297991a181f2fe647
push id8527
push userCallek@gmail.com
push dateThu, 11 Jan 2018 21:05:50 +0000
treeherdermozilla-beta@95342d212a7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbechen, gerald
bugs1428184
milestone59.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 1428184 - copy mStreams before iterating the array. r=bechen,gerald 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
@@ -719,26 +719,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) {