Skip to content

fix(windows): prevent crash on app exit caused by WinRT COM release d…#2838

Open
Y-PLONI wants to merge 6 commits into
pichillilorenzo:masterfrom
Otzaria:master
Open

fix(windows): prevent crash on app exit caused by WinRT COM release d…#2838
Y-PLONI wants to merge 6 commits into
pichillilorenzo:masterfrom
Otzaria:master

Conversation

@Y-PLONI

@Y-PLONI Y-PLONI commented May 17, 2026

Copy link
Copy Markdown

…uring DLL unload

Static WinRT/Composition objects (ICompositor, IDispatcherQueueController, GraphicsContext, RoHelper) were held in static smart pointers (winrt::com_ptr, std::unique_ptr, std::shared_ptr). Their destructors called Release() and RoUninitialize() during DLL_PROCESS_DETACH, after the WinRT DLLs had already started unloading, triggering RaiseFailFastException in KernelBase.dll and causing Windows Error Reporting to appear on exit.

Changes:

  • Convert all four static WinRT/COM resource pointers to raw pointers so no RAII destructor runs Release() or RoUninitialize() at static-destruction time; the OS reclaims them at process exit instead.
  • Add instance_count_ (protected by shared_resources_mutex_) to track the number of live InAppWebViewManager instances and trigger cleanup only when the last one is destroyed.
  • Add detachSharedResourcesForShutdown() which nulls all raw pointers under the mutex (without delete/Release) and returns the dispatcher queue controller for subsequent use.
  • Add shutdownDispatcherQueue() which calls ShutdownQueueAsync() outside the mutex and intentionally leaks both the IAsyncAction and the controller as process-lifetime objects, avoiding any Release() race with WinRT teardown.
  • Remove the earlier AddRef() workaround on ICompositor, which did not actually prevent the crash.
  • Replace the std::unique_ptr/shared_ptr allocations of RoHelper and GraphicsContext with raw new, consistent with the process-lifetime-leak design for all shared resources.

Fixes: #2733

Connection with issue(s)

Resolve issue #???

Connected to #???

Testing and Review Notes

Screenshots or Videos

To Do

  • double check the original issue to confirm it is fully satisfied
  • add testing notes and screenshots in PR description to help guide reviewers
  • request the "UX" team perform a design review (if/when applicable)

…uring DLL unload

Static WinRT/Composition objects (ICompositor, IDispatcherQueueController,
GraphicsContext, RoHelper) were held in static smart pointers
(winrt::com_ptr, std::unique_ptr, std::shared_ptr). Their destructors called
Release() and RoUninitialize() during DLL_PROCESS_DETACH, after the WinRT
DLLs had already started unloading, triggering RaiseFailFastException in
KernelBase.dll and causing Windows Error Reporting to appear on exit.

Changes:
- Convert all four static WinRT/COM resource pointers to raw pointers so
  no RAII destructor runs Release() or RoUninitialize() at static-destruction
  time; the OS reclaims them at process exit instead.
- Add instance_count_ (protected by shared_resources_mutex_) to track the
  number of live InAppWebViewManager instances and trigger cleanup only when
  the last one is destroyed.
- Add detachSharedResourcesForShutdown() which nulls all raw pointers under
  the mutex (without delete/Release) and returns the dispatcher queue
  controller for subsequent use.
- Add shutdownDispatcherQueue() which calls ShutdownQueueAsync() outside
  the mutex and intentionally leaks both the IAsyncAction and the controller
  as process-lifetime objects, avoiding any Release() race with WinRT teardown.
- Remove the earlier AddRef() workaround on ICompositor, which did not
  actually prevent the crash.
- Replace the std::unique_ptr/shared_ptr allocations of RoHelper and
  GraphicsContext with raw new, consistent with the process-lifetime-leak
  design for all shared resources.

Fixes: pichillilorenzo#2733

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@probot-autolabeler probot-autolabeler Bot added iOS linux Linux platform macOS macOS platform windows Windows platform labels May 17, 2026
@probot-autolabeler probot-autolabeler Bot added android platform_interface Platform Interface plugin flutter_inappwebview plugin web labels Jun 10, 2026
Y-PLONI and others added 4 commits June 10, 2026 21:31
Batching scroll sends to one platform message per Flutter frame added
0-16ms of variable latency, and in a busy app a delayed or skipped UI
frame held trackpad input back and then released it in a burst -
scrolling that intermittently ignored the fingers and caught up a
moment later. Chromium coalesces a per-event wheel stream by itself
(exactly what high-resolution mouse wheels produce natively), so the
frame-tied batching only added jitter.

Measured with the in-repo smoothness probe (real WebView2, identical
synthetic drag): end-to-end latency dropped from 81ms to 56ms and step
jitter improved (stdStep 0.66px -> 0.52px, 0% stalled frames) with
Chromium smooth scrolling at its default.

Also stop the synthetic fling on PointerScrollInertiaCancelEvent, which
the engine sends when the user touches the trackpad during inertia.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1:1 finger-to-page tracking was reported to feel sluggish compared to
native precision-touchpad scrolling. Pan deltas (and synthetic fling
distances, which pass through the same conversion) are now scaled by a
felt gain of 1.5 on top of the measured 120-units-per-100px Chromium
wheel calibration, so the page moves 1.5x the finger travel.
_kTrackpadGain is the single knob for tuning scroll speed.

A synthetic-touch (SendPointerInput PT_TOUCH) alternative was prototyped
and measured against a real WebView2: it gives native direct
manipulation and Chromium fling, but WebView2 only begins a touch
scroll after ~34px of movement per gesture (282ms begin latency,
gestures under ~30px scroll nothing). That dead zone kills the small
precise scrolls a reading app lives on, so the wheel-injection path
stays.

Measured after this change (in-repo smoothness probe, real WebView2):
page/finger = 1.50 exactly, 0% stalled frames, stdStep 0.44px,
42ms latency, and 8-40px micro-gestures all scroll at full gain.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

android iOS linux Linux platform macOS macOS platform platform_interface Platform Interface plugin flutter_inappwebview plugin web windows Windows platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Windows]Application Crash on Process Exit When Using flutter_inappwebview Plugin on Windows

1 participant