Guard Control UpdateAppearance/Settings against UAF (#17770)

When you close a window, it naturally loses focus.

We were trying to use members of the control to update its appearance on
focus loss after it got torn down.

Closes #17520

(cherry picked from commit eabebc4cb227293df4839f75bd195b04992ba506)
Service-Card-Id: PVTI_lADOAF3p4s4AmhmszgSIM6I
Service-Version: 1.21
This commit is contained in:
Dustin L. Howett 2024-08-22 12:55:49 -05:00 committed by Dustin L. Howett
parent f2e9041aa0
commit cf7a6617ab

View File

@ -674,11 +674,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// terminal.
co_await wil::resume_foreground(Dispatcher());
_core.UpdateSettings(settings, unfocusedAppearance);
if (auto strongThis{ weakThis.get() })
{
_core.UpdateSettings(settings, unfocusedAppearance);
_UpdateSettingsFromUIThread();
_UpdateSettingsFromUIThread();
_UpdateAppearanceFromUIThread(_focused ? _core.FocusedAppearance() : _core.UnfocusedAppearance());
_UpdateAppearanceFromUIThread(_focused ? _core.FocusedAppearance() : _core.UnfocusedAppearance());
}
}
// Method Description:
@ -687,10 +690,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// - newAppearance: the new appearance to set
winrt::fire_and_forget TermControl::UpdateAppearance(IControlAppearance newAppearance)
{
auto weakThis{ get_weak() };
// Dispatch a call to the UI thread
co_await wil::resume_foreground(Dispatcher());
_UpdateAppearanceFromUIThread(newAppearance);
if (auto strongThis{ weakThis.get() })
{
_UpdateAppearanceFromUIThread(newAppearance);
}
}
// Method Description: