diff --git a/layout/base/nsDocumentViewer.cpp b/layout/base/nsDocumentViewer.cpp index 2a0aedac6..91fedb0ff 100644 --- a/layout/base/nsDocumentViewer.cpp +++ b/layout/base/nsDocumentViewer.cpp @@ -2095,19 +2095,24 @@ nsDocumentViewer::Hide(void) mPresShell->CaptureHistoryState(getter_AddRefs(layoutState)); } - DestroyPresShell(); + { + // Do not run ScriptRunners queued by DestroyPresShell() in the intermediate + // state before we're done destroying PresShell, PresContext, ViewManager, etc. + nsAutoScriptBlocker scriptBlocker; + DestroyPresShell(); - DestroyPresContext(); + DestroyPresContext(); - mViewManager = nullptr; - mWindow = nullptr; - mDeviceContext = nullptr; - mParentWidget = nullptr; + mViewManager = nullptr; + mWindow = nullptr; + mDeviceContext = nullptr; + mParentWidget = nullptr; - nsCOMPtr base_win(mContainer); + nsCOMPtr base_win(mContainer); - if (base_win && !mAttachedToParent) { - base_win->SetParentWidget(nullptr); + if (base_win && !mAttachedToParent) { + base_win->SetParentWidget(nullptr); + } } return NS_OK;