microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.27k stars 674 forks source link

Regression v1.6: App crash when changing system theme after closing one of the windows #9888

Closed dotMorten closed 1 week ago

dotMorten commented 1 month ago

Describe the bug

If I have a window set to AppWindow.TitleBar.ExtendsContentIntoTitleBar = true, and I close that window (but other WinUI windows are still live), then changing the system theme, the following crash is observed:

Microsoft.UI.Xaml.UnhandledExceptionEventArgs: The given object has already been closed / disposed and may no longer be used."

Steps to reproduce the bug

  1. Unzip and run the reproducer app: App6.zip
  2. Once app is launched, a startup window will appear for 2 seconds, and then a new window will appear.
  3. When the second window has appeared, change the System theme to/from dark/light mode (or any other theme changes for that matter), and observe the application crashing.

Expected behavior

No app crash.

Screenshots

No response

NuGet package version

Any of these 3 versions reproduces the issue: WinUI 3 - Windows App SDK 1.6 Experimental 1: 1.6.240531000-experimental1 WinUI 3 - Windows App SDK 1.6 Experimental 2: 1.6.240701003-experimental2 WinUI 3 - Windows App SDK 1.6 Preview 1: 1.6.240807006-preview1

Windows version

Windows 11 (22H2): Build 22621

Additional context

This crash prevents users from creating things like splash screens or startup screens, or any type of temporary windows. Setting ExtendsContentIntoTitleBar to false in the StartupWindow class avoids the issue, but means you can't use the new titlebar. This is a regression from 1.5.240802000 where this is a non-issue.

Full callstack:

>   App6.dll!App6.App.InitializeComponent.AnonymousMethod__4_2(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) Line 75  C#
    Microsoft.WinUI.dll!WinRT._EventSource_global__Microsoft_UI_Xaml_UnhandledExceptionEventHandler.EventState.GetEventInvoke.AnonymousMethod__1_0(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) Line 2191    C#
    Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.UnhandledExceptionEventHandler.Do_Abi_Invoke(nint thisPtr, nint sender, nint e) Line 29982    C#
    [Native to Managed Transition]  
    Microsoft.ui.xaml.dll!DirectUI::CFTMEventSource<ABI::Microsoft::UI::Xaml::IUnhandledExceptionEventHandler,ABI::Microsoft::UI::Xaml::IApplication,ABI::Microsoft::UI::Xaml::IUnhandledExceptionEventArgs>::Raise(ABI::Microsoft::UI::Xaml::IApplication * pSource, ABI::Microsoft::UI::Xaml::IUnhandledExceptionEventArgs * pArgs) Line 319  C++
    Microsoft.ui.xaml.dll!DirectUI::FrameworkApplication::RaiseUnhandledExceptionEvent(HRESULT hrToReport, HSTRING__ * hstrMessage, bool * pfHandled) Line 909  C++
    Microsoft.ui.xaml.dll!DirectUI::ErrorHelper::RaiseUnhandledExceptionEvent(HRESULT hrToReport, const xstring_ptr & strMessage, bool * pfHandled) Line 289    C++
    Microsoft.ui.xaml.dll!DirectUI::ErrorHelper::ProcessUnhandledError(DirectUI::ErrorInfo & errorInfo, bool fSkipFailFastIfNoErrorContext, bool * pfHandled) Line 552  C++
    Microsoft.ui.xaml.dll!DirectUI::FinalUnhandledErrorDetectedRegistration::OnFinalUnhandledErrorDetected(IInspectable * pSender, ABI::Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs * pArgs) Line 913 C++
    [Inline Frame] Microsoft.ui.xaml.dll!Microsoft::WRL::Callback::__l2::<lambda_1>::operator()(IInspectable * &&) Line 471 C++
    Microsoft.ui.xaml.dll!Microsoft::WRL::Details::DelegateArgTraits<long (__stdcall ABI::Windows::Foundation::IEventHandler_impl<ABI::Windows::Foundation::Internal::AggregateType<ABI::Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *,ABI::Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *>>::*)(IInspectable *,ABI::Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *)>::DelegateInvokeHelper<Microsoft::WRL::Implements<Microsoft::WRL::RuntimeClassFlags<2>,ABI::Windows::Foundation::IEventHandler<ABI::Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,Microsoft::WRL::FtmBase>,`Microsoft::WRL::Callback<Microsoft::WRL::Implements<Microsoft::WRL::RuntimeClassFlags<2>,ABI::Windows::Foundation::IEventHandler<ABI::Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,Microsoft::WRL::FtmBase>,long __stdcall(IInspectable *,ABI::Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *)>'::`2'::<lambda_1>,1,IInspectable *,ABI::Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *>::Invoke(IInspectable * <args_0>, ABI::Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs * <args_1>) Line 354 C++
    twinapi.appcore.dll!Windows::Internal::Details::GitInvokeHelper<struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,class Windows::Internal::GitPtr,2>::Invoke(struct IInspectable *,struct Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *)    Unknown
    twinapi.appcore.dll!Windows::ApplicationModel::Core::UnhandledErrorInvokeHelper::Invoke(struct IInspectable *,struct Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *)   Unknown
    twinapi.appcore.dll!Microsoft::WRL::InvokeTraits<2>::InvokeDelegates<class <lambda_623fa8e2d45489ddf21c227ec4a97ebd>,struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *> >(class <lambda_623fa8e2d45489ddf21c227ec4a97ebd>,class Microsoft::WRL::Details::EventTargetArray *,class Microsoft::WRL::EventSource<struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,struct Microsoft::WRL::InvokeModeOptions<2> > *) Unknown
    twinapi.appcore.dll!Microsoft::WRL::EventSource<struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,struct Microsoft::WRL::InvokeModeOptions<2> >::DoInvoke<class <lambda_623fa8e2d45489ddf21c227ec4a97ebd> >(class <lambda_623fa8e2d45489ddf21c227ec4a97ebd>)  Unknown
    twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplication::ForwardLocalError(struct IRestrictedErrorInfo *)  Unknown
    twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationFactory::ForwardLocalError(struct IRestrictedErrorInfo *)   Unknown
    combase.dll!CallErrorForwarder(void * hProcess, int fLocal, IRestrictedErrorInfo * pRestrictedErrorInfo) Line 1614  C++
    combase.dll!RoReportFailedDelegate(IUnknown * punkDelegate, IRestrictedErrorInfo * pRestrictedErrorInfo) Line 1704  C++
    Windows.UI.dll!Microsoft::WRL::InvokeTraits<-2>::InvokeDelegates<class <lambda_4763a770c5863e9f1c3b3a2e7248a535>,struct Windows::Foundation::ITypedEventHandler<class Windows::UI::ViewManagement::UISettings *,struct IInspectable *> >(class <lambda_4763a770c5863e9f1c3b3a2e7248a535>,class Microsoft::WRL::Details::EventTargetArray *,class Microsoft::WRL::EventSource<struct Windows::Foundation::ITypedEventHandler<class Windows::UI::ViewManagement::UISettings *,struct IInspectable *>,struct Microsoft::WRL::InvokeModeOptions<-2> > *)    Unknown
    Windows.UI.dll!Microsoft::WRL::EventSource<struct Windows::Foundation::ITypedEventHandler<class Windows::UI::ViewManagement::UISettings *,struct IInspectable *>,struct Microsoft::WRL::InvokeModeOptions<-2> >::DoInvoke<class <lambda_4763a770c5863e9f1c3b3a2e7248a535> >(class <lambda_4763a770c5863e9f1c3b3a2e7248a535>)    Unknown
    Windows.UI.dll!Windows::UI::ViewManagement::UISettings::OnThemeColorsChanged(void)  Unknown
    Windows.UI.dll!Microsoft::WRL::Details::DelegateArgTraits<long (__stdcall Windows::Foundation::IEventHandler_impl<IInspectable *>::*)(IInspectable *,IInspectable *)>::DelegateInvokeHelper<Microsoft::WRL::Implements<Microsoft::WRL::RuntimeClassFlags<2>,Windows::Foundation::IEventHandler<IInspectable *>,Microsoft::WRL::FtmBase>,<lambda_aef84e8c5581b4773203fc04d29e5780> &,-1,IInspectable *,IInspectable *>::Invoke() Unknown
    Windows.UI.Immersive.dll!Microsoft::WRL::InvokeTraits<-2>::InvokeDelegates<class <lambda_cc5672c12bf54cf9213cf7b92da15a57>,struct Windows::Foundation::IEventHandler<struct IInspectable *> >(class <lambda_cc5672c12bf54cf9213cf7b92da15a57>,class Microsoft::WRL::Details::EventTargetArray *,class Microsoft::WRL::EventSource<struct Windows::Foundation::IEventHandler<struct IInspectable *>,struct Microsoft::WRL::InvokeModeOptions<-2> > *)    Unknown
    Windows.UI.Immersive.dll!Microsoft::WRL::EventSource<struct Windows::Foundation::IEventHandler<struct IInspectable *>,struct Microsoft::WRL::InvokeModeOptions<-2> >::DoInvoke<class <lambda_cc5672c12bf54cf9213cf7b92da15a57> >(class <lambda_cc5672c12bf54cf9213cf7b92da15a57>)   Unknown
    Windows.UI.Immersive.dll!ApplicationTheme::ThemeColorsChangedEvent::RegistryChangeCallback_ColorChanged(class ApplicationTheme::ThemeColorsChangedEvent *)  Unknown
    Windows.UI.Immersive.dll!wistd::__function::__func<<lambda_535a52ad28e30157273f31aba60fbe8b>,void __stdcall(enum wil::RegistryChangeKind)>::operator()()    Unknown
    Windows.UI.Immersive.dll!wistd::function<void (enum wil::RegistryChangeKind)>::operator()(enum wil::RegistryChangeKind) Unknown
    Windows.UI.Immersive.dll!wil::registry_watcher_t<class wil::details::unique_storage<struct wil::details::resource_policy<struct wil::details::registry_watcher_state *,void (*)(struct wil::details::registry_watcher_state *),&wil::details::delete_registry_watcher_state(struct wil::details::registry_watcher_state *),struct wistd::integral_constant<unsigned int,2>,struct wil::details::registry_watcher_state *,struct wil::details::registry_watcher_state *,0,std::nullptr_t> >,struct wil::err_returncode_policy>::callback(struct _TP_CALLBACK_INSTANCE *,void *,struct _TP_WAIT *,long)   Unknown
codendone commented 3 weeks ago

Breaking on WinRT errors (RoOriginateError, doable via !winrterr break in WinDbg) Shows the exception originating from the TitleBar code:

02 combase!RoOriginateErrorW
03 Microsoft_UI_Input!Microsoft::WRL2::ContextRuntimeClass::ReportDisposed
04 Microsoft_UI_Input!InputNonClientPointerSourceWinRT::Api::GetRegionRects
05 Microsoft_UI_Windowing_Core!CTitleBar::UpdateColors
06 Microsoft_UI_Windowing_Core!CTitleBar::Update
07 Microsoft_UI_Windowing_Core!CTitleBar::OnUISettingsColorValuesChanged
dotMorten commented 3 weeks ago

@codendone Thanks Mike. I had a feeling it was titlebar related since ExtendsContentIntoTitleBar triggers the issue. I was trying to compare 1.5.x branch with 1.6.x but looks like the source tree history got all messed up :(

codendone commented 3 weeks ago

Note: It appears the app will continue running if continuing past this exception (or if no debugger is attached). But that is scary in the context of an UnhandledException being raised.

MartyIX commented 2 weeks ago

I was trying to compare 1.5.x branch with 1.6.x but looks like the source tree history got all messed up :(

I reported it here https://github.com/microsoft/microsoft-ui-xaml/discussions/9911 and it appears it works now: https://github.com/microsoft/microsoft-ui-xaml/compare/winui3/release/1.5.6...winui3/release/1.6-preview2