Bug 1428184 - copy mStreams before iterating the array.
It is bad to modify the array while iterating it.
MozReview-Commit-ID: JbpoRmM7GqP
--- 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) {