@@ -2044,7 +2044,15 @@ namespace winrt::TerminalApp::implementation
20442044 if (!_displayingCloseDialog)
20452045 {
20462046 _displayingCloseDialog = true ;
2047+
2048+ const auto weak = get_weak ();
20472049 auto warningResult = co_await _ShowQuitDialog ();
2050+ const auto strong = weak.get ();
2051+ if (!strong)
2052+ {
2053+ co_return ;
2054+ }
2055+
20482056 _displayingCloseDialog = false ;
20492057
20502058 if (warningResult != ContentDialogResult::Primary)
@@ -3084,8 +3092,12 @@ namespace winrt::TerminalApp::implementation
30843092 // - eventArgs: the arguments specifying how to set the progress indicator
30853093 safe_void_coroutine TerminalPage::_SetTaskbarProgressHandler (const IInspectable /* sender*/ , const IInspectable /* eventArgs*/ )
30863094 {
3095+ const auto weak = get_weak ();
30873096 co_await wil::resume_foreground (Dispatcher ());
3088- SetTaskbarProgress.raise (*this , nullptr );
3097+ if (const auto strong = weak.get ())
3098+ {
3099+ SetTaskbarProgress.raise (*this , nullptr );
3100+ }
30893101 }
30903102
30913103 // Method Description:
@@ -3158,6 +3170,12 @@ namespace winrt::TerminalApp::implementation
31583170 {
31593171 co_return ;
31603172 }
3173+
3174+ const auto weak = get_weak ();
3175+ const auto dispatcher = Dispatcher ();
3176+
3177+ // All of the code until resume_foreground is static and
3178+ // doesn't touch `this`, so we don't need weak/strong_ref.
31613179 co_await winrt::resume_background ();
31623180
31633181 // no packages were found, nothing to suggest
@@ -3175,7 +3193,12 @@ namespace winrt::TerminalApp::implementation
31753193 suggestions.emplace_back (fmt::format (FMT_COMPILE (L" winget install --id {} -s winget" ), pkg.CatalogPackage ().Id ()));
31763194 }
31773195
3178- co_await wil::resume_foreground (Dispatcher ());
3196+ co_await wil::resume_foreground (dispatcher);
3197+ const auto strong = weak.get ();
3198+ if (!strong)
3199+ {
3200+ co_return ;
3201+ }
31793202
31803203 auto term = _GetActiveControl ();
31813204 if (!term)
@@ -3255,6 +3278,9 @@ namespace winrt::TerminalApp::implementation
32553278 // UI) thread. This is IMPORTANT, because the Windows.Storage API's
32563279 // (used for retrieving the path to the file) will crash on the UI
32573280 // thread, because the main thread is a STA.
3281+ //
3282+ // NOTE: All remaining code of this function doesn't touch `this`, so we don't need weak/strong_ref.
3283+ // NOTE NOTE: Don't touch `this` when you make changes here.
32583284 co_await winrt::resume_background ();
32593285
32603286 auto openFile = [](const auto & filePath) {
@@ -4633,12 +4659,18 @@ namespace winrt::TerminalApp::implementation
46334659 // - sender: the ICoreState instance containing the connection state
46344660 // Return Value:
46354661 // - <none>
4636- safe_void_coroutine TerminalPage::_ConnectionStateChangedHandler (const IInspectable& sender, const IInspectable& /* args*/ ) const
4662+ safe_void_coroutine TerminalPage::_ConnectionStateChangedHandler (const IInspectable& sender, const IInspectable& /* args*/ )
46374663 {
46384664 if (const auto coreState{ sender.try_as <winrt::Microsoft::Terminal::Control::ICoreState>() })
46394665 {
46404666 const auto newConnectionState = coreState.ConnectionState ();
4667+ const auto weak = get_weak ();
46414668 co_await wil::resume_foreground (Dispatcher ());
4669+ const auto strong = weak.get ();
4670+ if (!strong)
4671+ {
4672+ co_return ;
4673+ }
46424674
46434675 _adjustProcessPriorityThrottled->Run ();
46444676
0 commit comments