Skip to content

fix: WebView2 ACCESS_VIOLATION on rapid dispose/create cycles - #325

Open
ymoage wants to merge 1 commit into
jnschulze:mainfrom
ymoage:fix/webview2-access-violation
Open

ymoage wants to merge 1 commit into
jnschulze:mainfrom
ymoage:fix/webview2-access-violation

Conversation

@ymoage

@ymoage ymoage commented Jan 2, 2026 •

Copy link
Copy Markdown

Summary

  • Fix ACCESS_VIOLATION crash when rapidly disposing and creating WebView2 instances
  • Properly clean up event handlers and callbacks before controller destruction
  • Control destruction order in WebviewBridge to prevent dangling references

Problem

When a WebView is disposed and a new one is created immediately after, an ACCESS_VIOLATION can occur. This happens because:

  1. Event handlers are not unregistered before Close()
  2. Callbacks capturing this pointer are not cleared before destruction
  3. Destruction order of unique_ptr members causes dangling references

Changes

  • webview.h: Add ClearCallbacks() public method and UnregisterEventHandlers() private method
  • webview.cc: Call UnregisterEventHandlers() and ClearCallbacks() before Close() in destructor
  • webview_bridge.cc: Explicitly control destruction order (clear callbacks first, then texture_bridge_, then webview_)
  • webview.dart: Add setMuted() method for pre-dispose cleanup

Testing

Tested with 100+ rapid dispose/create cycles without crash (previously crashed within 2-3 cycles).

Known Issues (minor, deferred)

  • graphics_context.cc: Minor COM object leak (once per app launch) - added TODO comment
  • webview_bridge.cc: Defensive callback clearing suggestion - added TODO comment

Root cause: Event handlers and callbacks not properly cleaned up
before WebView2 controller destruction.

Changes:
- webview.h: Add ClearCallbacks() public method and UnregisterEventHandlers()
- webview.cc: Unregister event handlers and clear callbacks before Close()
- webview_bridge.cc: Control destruction order explicitly
- webview.dart: Add setMuted() method for pre-dispose cleanup

Tested: 100+ stress test cycles without crash

Known issues (minor, deferred):
- graphics_context.cc: COM object leak (once per launch)
- webview_bridge.cc: Defensive callback clearing (not causing issues)
@ymoage

ymoage commented Nov 4, 2026

Copy link
Copy Markdown
Author

Apologies to the maintainers and anyone following this PR.

While this PR was open, I kept pushing unrelated work from my own app to the same branch (fix/webview2-access-violation). That work was never meant for upstream. As a result, the PR grew to 25 commits, and several commit messages included references like #26, #298 and #304. Those were my own internal issue numbers, but GitHub linked them to unrelated issues in this repository. I'm sorry for the noise this may have caused.

I have reset the branch so that the PR contains only the original fix (ACCESS_VIOLATION on rapid dispose/create, 1 commit). My app's changes now live on a separate branch in my fork, and I've set up my local repository so it can no longer push to this PR branch or to this repository by mistake.

If you would rather close this PR, that's completely fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant