question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebView2 in a Window crashes the app when the Window is closed.

See original GitHub issue

Describe the bug

I can open a Window with a WebView2 as the Content, but when I close the window (X button), the app crashes.

Steps to reproduce the bug

  1. Create a new WinUI3 app with C#
  2. Use the following in the csproj:
		<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.0.0-20211129.1-CI-experimental" />
		<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.0.0.30" />
  1. Add the code in the button click handler:
        private async void myButton_Click(object sender, RoutedEventArgs e)
        {
            var webView = new WebView2();
            await webView.EnsureCoreWebView2Async();
            webView.Source = new Uri("https://bing.com");
            var window = new Window();
            window.Content = webView;
            window.Activate();
        }
  1. Run the app.
  2. Click the button
  3. Close the window

Expected behavior: Able to open and close the window multiple times. Actual behavior: The app crashes when the window is closed.

Expected behavior

User is able to open and close the window multiple times.

Screenshots

No response

NuGet package version

No response

Windows app type

  • UWP
  • Win32

Device form factor

Desktop

Windows version

Windows 11 (21H2): Build 22000

Additional context

Exception details:

Exception thrown at 0x00007FFC55E0478C in WebViewWindow.exe: Microsoft C++ exception: winrt::hresult_error at memory location 0x0000004712BECF00.

Call Stack:

KernelBase.dll!RaiseException()	Unknown

vcruntime140.dll!CxxThrowException(void * pExceptionObject, const s__ThrowInfo * pThrowInfo) Line 75 C++ Microsoft.Web.WebView2.Core.dll!winrt::throw_hresult(struct winrt::hresult) Unknown Microsoft.Web.WebView2.Core.dll!winrt::impl::produce<struct winrt::Microsoft::Web::WebView2::Core::implementation::CoreWebView2Controller,struct winrt::Microsoft::Web::WebView2::Core::ICoreWebView2Controller>::put_Bounds(struct winrt::Windows::Foundation::Rect) Unknown Microsoft.UI.Xaml.Controls.dll!WebView2::CheckAndUpdateWebViewPosition(void) Unknown Microsoft.UI.Xaml.Controls.dll!WebView2::HandleRendered(struct winrt::Windows::Foundation::IInspectable const &,struct winrt::Windows::Foundation::IInspectable const &) Unknown Microsoft.UI.Xaml.Controls.dll!winrt::impl::delegate<struct winrt::Windows::Foundation::EventHandler<struct winrt::Windows::Foundation::IInspectable>,class <lambda_28e261f44abbbcdd11610b5631184807> >::Invoke(void *,void *) Unknown Microsoft.ui.xaml.dll!DirectUI::CEventSourceBase<struct DirectUI::IUntypedEventSource,struct ABI::Windows::Foundation::IEventHandler<struct IInspectable *>,struct IInspectable,struct IInspectable>::Raise(struct IInspectable *,struct IInspectable *) Unknown Microsoft.ui.xaml.dll!DirectUI::DXamlCore::RaiseEvent(class CDependencyObject *,class CEventArgs *,enum DirectUI::ManagedEvent) Unknown Microsoft.ui.xaml.dll!CCoreServices::NWDrawTree(class HWWalk *,class CWindowRenderTarget *,class VisualTree *,bool,bool *) Unknown Microsoft.ui.xaml.dll!CCoreServices::NWDrawMainTree(class CWindowRenderTarget *,bool,bool *) Unknown Microsoft.ui.xaml.dll!CWindowRenderTarget::Draw(class CCoreServices *,bool,bool *) Unknown Microsoft.ui.xaml.dll!CXcpBrowserHost::OnTick(void) Unknown Microsoft.ui.xaml.dll!CXcpDispatcher::Tick(void) Unknown Microsoft.ui.xaml.dll!CXcpDispatcher::OnReentrancyProtectedWindowMessage(struct HWND *,unsigned int,unsigned __int64,int64) Unknown Microsoft.ui.xaml.dll!CXcpDispatcher::WindowProc(struct HWND *,unsigned int,unsigned __int64,__int64) Unknown Microsoft.ui.xaml.dll!Microsoft::WRL::Details::DelegateArgTraits<long (__cdecl ABI::Windows::Foundation::ITypedEventHandler_impl<ABI::Windows::Foundation::Internal::AggregateType<ABI::Microsoft::UI::Dispatching::DispatcherQueueTimer *,ABI::Microsoft::UI::Dispatching::IDispatcherQueueTimer *>,IInspectable >::)(ABI::Microsoft::UI::Dispatching::IDispatcherQueueTimer *,IInspectable *)>::DelegateInvokeHelper<Microsoft::WRL::Implements<Microsoft::WRL::RuntimeClassFlags<2>,ABI::Windows::Foundation::ITypedEventHandler<ABI::Microsoft::UI::Dispatching::DispatcherQueueTimer *,IInspectable *>,Microsoft::WRL::FtmBase>,<lambda_3e1276e0d827edcdd41a81b89b1d2778> &,1,ABI::Microsoft::UI::Dispatching::IDispatcherQueueTimer *,IInspectable *>::Invoke() Unknown CoreMessagingXP.dll!Microsoft::WRL::Details::DelegateArgTraits<long ( Windows::Foundation::ITypedEventHandler_impl<struct Windows::Foundation::Internal::AggregateType<class Microsoft::UI::Dispatching::DispatcherQueueTimer *,struct Microsoft::UI::Dispatching::IDispatcherQueueTimer *>,struct IInspectable >::)(struct Microsoft::UI::Dispatching::IDispatcherQueueTimer *,struct IInspectable *)>::DelegateInvokeHelper<struct Microsoft::WRL::Implements<struct Microsoft::WRL::RuntimeClassFlags<2>,struct Windows::Foundation::ITypedEventHandler<class Microsoft::UI::Dispatching::DispatcherQueueTimer *,struct IInspectable *>,class Microsoft::WRL::FtmBase>,class <lambda_82cf8073f4f042d1a68771c460cb9f49>,-1,struct Microsoft::UI::Dispatching::IDispatcherQueueTimer *,struct IInspectable *>::Invoke(struct Microsoft::UI::Dispatching::IDispatcherQueueTimer *,struct IInspectable *) Unknown CoreMessagingXP.dll!Microsoft::WRL::InvokeTraits<-2>::InvokeDelegates<class <lambda_1e854da9c9ccd42f6138c3b007a32877>,struct Windows::Foundation::ITypedEventHandler<class Microsoft::UI::Dispatching::DispatcherQueueTimer *,struct IInspectable *> >(class <lambda_1e854da9c9ccd42f6138c3b007a32877>,class Microsoft::WRL::Details::EventTargetArray *,class Microsoft::WRL::EventSource<struct Windows::Foundation::ITypedEventHandler<class Microsoft::UI::Dispatching::DispatcherQueueTimer *,struct IInspectable *>,struct Microsoft::WRL::InvokeModeOptions<-2> > *) Unknown CoreMessagingXP.dll!Microsoft::UI::Dispatching::DispatcherQueueTimer::TimerCallback(void ) Unknown CoreMessagingXP.dll!CFlat::SehSafe::Execute<<lambda_654db17c35df07198786f0867aa10de6>>() Unknown CoreMessagingXP.dll!Microsoft::CoreUI::Dispatch::TimeoutHandler::ImportAdapter$(class CFlat::Box$1<struct CFlat::FunctionPointerAndUserData$1<long ()(void *)> > *) Unknown CoreMessagingXP.dll!Microsoft::CoreUI::Dispatch::TimeoutManager::Callback_OnDispatch(void) Unknown CoreMessagingXP.dll!Microsoft::CoreUI::Dispatch::Dispatcher::Callback_DispatchNextItem(void) Unknown CoreMessagingXP.dll!Microsoft::CoreUI::Dispatch::EventLoop::Callback_RunCoreLoop(enum Microsoft::CoreUI::Dispatch::RunMode) Unknown CoreMessagingXP.dll!Microsoft::CoreUI::Dispatch::SystemCoreMessaging::Callback_OnGroupDispatch(enum Microsoft::CoreUI::Dispatch::SystemCoreMessaging$GroupPriority,struct CFlat::Ref<struct System::IntPtr>) Unknown CoreMessagingXP.dll!Microsoft::CoreUI::Dispatch::SystemCoreMessaging::NoContext_GroupDispatchHandler(enum Microsoft::CoreUI::Dispatch::SystemCoreMessaging$GroupPriority,void *) Unknown CoreMessagingXP.dll!Microsoft::CoreUI::Dispatch::SystemCoreMessaging::NoContext_NormalPriorityDispatchHandler(void *) Unknown CoreMessaging.dll!CFlat::SehSafe::Execute<<lambda_72be19825923cb708ba996fdf6b354f1>>() Unknown CoreMessaging.dll!Microsoft::CoreUI::DispatchGroupHandler::ImportAdapter$() Unknown CoreMessaging.dll!Microsoft::CoreUI::BatchAgnosticDispatchGroup::Callback_OnDispatchCore() Unknown CoreMessaging.dll!Microsoft::CoreUI::Dispatch::EventLoop::Callback_RunCoreLoop() Unknown CoreMessaging.dll!Microsoft::CoreUI::Dispatch::UserAdapter::OnUserDispatch() Unknown CoreMessaging.dll!Microsoft::CoreUI::Dispatch::UserAdapter::DoWork() Unknown CoreMessaging.dll!Microsoft::CoreUI::Dispatch::UserAdapter::WindowProc() Unknown user32.dll!UserCallWinProcCheckWow() Unknown user32.dll!DispatchClientMessage() Unknown user32.dll!__fnDWORD() Unknown ntdll.dll!KiUserCallbackDispatcherContinue() Unknown win32u.dll!NtUserGetMessage() Unknown user32.dll!GetMessageW() Unknown Microsoft.ui.xaml.dll!DirectUI::FrameworkApplication::RunDesktopWindowMessageLoop(void) Unknown Microsoft.ui.xaml.dll!DirectUI::FrameworkApplication::StartDesktop(void) Unknown Microsoft.ui.xaml.dll!DirectUI::FrameworkApplicationFactory::Start(struct ABI::Microsoft::UI::Xaml::IApplicationInitializationCallback *) Unknown

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
LiuChangFreemancommented, Apr 2, 2022

You must close the Webview when window is closed

 public MainWindow()
{
    this.InitializeComponent();
    this.Closed += (o, e) =>
    {
        MainWebView.Close();
    };
}
0reactions
mlynchcommented, Apr 30, 2023

Can confirm this is a regression in 1.3.x as it does not happen when using 1.2.230313.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webview2 crashes when calling document.write on newly ...
Everything seems to work normally - it appears to fail with the first window trying to write to the new window's document -...
Read more >
My app crashes when closing my seccond window
My app crashes when closing my seccond window. This is all I get. No information at all. I am using webview in my...
Read more >
Msedgewebview2 seems buggy, why window defender ...
Try repairing webview2 by going to settings/apps then scrolling down until you reach Microsoft edge web view 2 runtime. ... A popup will...
Read more >
Microsoft Edge Webview2 stopped working - keeps crashing
This is happening daily, latest updates to Office365 16.0.15601.20148 and Windows 11 Pro 22000.978 made no difference. Latest Edge/ WebView ...
Read more >
last version crashes on starting application (WebView2 )
As a start, run Windows Update and after that reinstall all the Adobe apps. Mylenium. Votes. 0 Upvote.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found