Fix crashes during document loading and improve error handling#12
Merged
Conversation
The EXCEPTION_ACCESS_VIOLATION in SetSidebarVisibility+0x48
(ReplaceDocumentInCurrentTab) was a use-after-free on MainWindow*.
For command-line (or startup) loads we use the sync path:
CreateAndShowMainWindow (window visible) ->
CreateControllerForEngineOrFile (can take tens of seconds for
large/complex EPUBs on slow CPUs) ->
LoadDocumentFinish / Replace... / SetSidebarVisibility
If the user closes the (frozen) window, WM_CLOSE is queued.
During tab insertion, RedrawAll, Show/UpdateWindow etc. in the
finish path the close can be processed synchronously via
DestroyWindow, which calls DeleteMainWindow and tears down the
win/tabs/DisplayModel while the load code is still on the stack.
Added IsMainWindowValid(win) && !win->isBeingClosed guards at the
entry to ReplaceDocumentInCurrentTab, after tab creation, before/after
the Replace call, and immediately before SetSidebarVisibility.
This follows the existing pattern in LoadModelIntoTab and the
async load path (LoadDocumentAsyncFinish).
The original report was crash 86.227.233.173 (EPUB, 327 pages,
~38s load, Win10 on Pentium N4200).
No ailog.txt to commit.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Defer the SaveSettings() call that happens at the end of LoadDocumentFinish() by posting it via uitask::Post. This prevents SaveSettings() (which walks all tabs and calls tab->ctrl->GetDisplayState) from running while other documents are still being loaded or closed via DDE command storm. As a defensive measure, snapshot the list of tabs before iterating in SaveSettings(), since the call can now happen asynchronously relative to tab open/close operations. This addresses crashes such as: https://www.sumatrapdfreader.org/crash/2026-05-20/89bcb2fbc000001.txt The AV occurred in UpdateTabFileDisplayStateForTab (line 567) with a garbage tab->ctrl pointer (e.g. FFFFFFFF000000A1) under heavy load combined with 3rd-party window hooks (MarkAny Document SAFER + GridWndHookLM64.dll). Also ran clang-format. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Applied fixes from sumatrapdfreader:master commits 032a973 and fcf280b: - fix crash when user closes window during slow initial document load with IsMainWindowValid() guards at key points in ReplaceDocumentInCurrentTab - Fix crash with dangling tab->ctrl during rapid document loading by deferring SaveSettings() via uitask::Post() for async-safe operation Integrated UI-Improvements improvements: - ReplaceDocumentInCurrentTab now returns bool for error signaling - Proper cleanup of args->ctrl on early-return paths to prevent leaks - Addressed Copilot review feedback on resource safety and re-entrancy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request addresses stability issues in the document loading workflow, specifically targeting crashes related to window and tab management during rapid document operations. The changes add additional safety checks and clarify the rationale for asynchronous settings saving.
Crash prevention and stability improvements:
LoadDocumentFinishto ensure that theMainWindowis still valid and not in the process of closing before proceeding with further operations, preventing potential crashes during rapid document or tab transitions. [1] [2]Documentation and code clarity:
SaveSettingsVoidto clarify that asynchronous settings saving is necessary to avoid crashes caused by dangling pointers to tab controls during rapid DDE (Dynamic Data Exchange) opens and hooks.