Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the window thread from the list of threads before nulling the AppHost #15231

Merged
merged 3 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/cascadia/WindowsTerminal/WindowEmperor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void WindowEmperor::_createNewWindowThread(const Remoting::WindowRequestedArgs&
std::thread t([weakThis, window]() {
try
{
const auto cleanup = wil::scope_exit([&]() {
auto cleanup = wil::scope_exit([&]() {
if (auto self{ weakThis.lock() })
{
self->_windowExitedHandler(window->Peasant().GetID());
Expand All @@ -155,6 +155,16 @@ void WindowEmperor::_createNewWindowThread(const Remoting::WindowRequestedArgs&
}

window->RunMessagePump();

// Manually trigger the cleanup callback. This will ensure that we
// remove the window from our list of windows, before we release the
// AppHost (and subsequently, the host's Logic() member that we use
// elsewhere).
cleanup.reset();

// Now that we no longer care about this thread's window, let it
// release it's app host and flush the rest of the XAML queue.
window->RundownForExit();
}
CATCH_LOG()
});
Expand Down
6 changes: 4 additions & 2 deletions src/cascadia/WindowsTerminal/WindowThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ int WindowThread::RunMessagePump()
// Enter the main window loop.
const auto exitCode = _messagePump();
// Here, the main window loop has exited.
return exitCode;
}

void WindowThread::RundownForExit()
{
_host = nullptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, (re)move the _host = nullptr?

// !! LOAD BEARING !!
//
Expand All @@ -54,8 +58,6 @@ int WindowThread::RunMessagePump()
::DispatchMessageW(&msg);
}
}

return exitCode;
}

winrt::TerminalApp::TerminalWindow WindowThread::Logic()
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/WindowsTerminal/WindowThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class WindowThread : public std::enable_shared_from_this<WindowThread>
winrt::TerminalApp::TerminalWindow Logic();
void CreateHost();
int RunMessagePump();
void RundownForExit();

winrt::Microsoft::Terminal::Remoting::Peasant Peasant();

Expand Down