WebView2 in a Window crashes the app when the Window is closed.
See original GitHub issueDescribe 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
- Create a new WinUI3 app with C#
- 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" />
- 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();
}
- Run the app.
- Click the button
- 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:
- Created a year ago
- Reactions:2
- Comments:9 (1 by maintainers)

Top Related StackOverflow Question
You must close the Webview when window is closed
Can confirm this is a regression in 1.3.x as it does not happen when using
1.2.230313.1