From 089580d09a980f5fffa03e5c87c985d58d1faef8 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 25 Mar 2023 16:19:26 -0700 Subject: [PATCH] Windows: Always detach request threads. No point having a global if it's always detached after use. --- UI/MainScreen.cpp | 3 +-- Windows/main.cpp | 34 ++++++++++------------------------ 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index d5baa80ef1..7efc3cb27d 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -541,8 +541,7 @@ UI::EventReturn GameBrowser::LastClick(UI::EventParams &e) { UI::EventReturn GameBrowser::BrowseClick(UI::EventParams &e) { auto mm = GetI18NCategory("MainMenu"); - System_BrowseForFolder(mm->T("Choose folder"), [this](const std::string &value, int) { - std::string filename = value; + System_BrowseForFolder(mm->T("Choose folder"), [this](const std::string &filename, int) { this->SetPath(Path(filename)); }); return UI::EVENT_DONE; diff --git a/Windows/main.cpp b/Windows/main.cpp index ea0298ad27..dd1cb8f966 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -111,9 +111,6 @@ static std::string restartArgs; int g_activeWindow = 0; -// Used for all the system dialogs. -static std::thread g_dialogThread; - WindowsInputManager g_inputManager; int g_lastNumInstances = 0; @@ -496,24 +493,17 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string return true; } case SystemRequestType::INPUT_TEXT_MODAL: - if (g_dialogThread.joinable()) - g_dialogThread.join(); - - g_dialogThread = std::thread([=] { + std::thread([=] { std::string out; if (InputBox_GetString(MainWindow::GetHInstance(), MainWindow::GetHWND(), ConvertUTF8ToWString(param1).c_str(), param2, out)) { g_requestManager.PostSystemSuccess(requestId, out.c_str()); } else { g_requestManager.PostSystemFailure(requestId); } - }); - g_dialogThread.detach(); + }).detach(); return true; case SystemRequestType::BROWSE_FOR_IMAGE: - if (g_dialogThread.joinable()) - g_dialogThread.join(); - - g_dialogThread = std::thread([=] { + std::thread([=] { std::string out; if (W32Util::BrowseForFileName(true, MainWindow::GetHWND(), ConvertUTF8ToWString(param1).c_str(), nullptr, MakeFilter(L"All supported images (*.jpg *.jpeg *.png)|*.jpg;*.jpeg;*.png|All files (*.*)|*.*||").c_str(), L"jpg", out)) { @@ -521,8 +511,7 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string } else { g_requestManager.PostSystemFailure(requestId); } - }); - g_dialogThread.detach(); + }).detach(); return true; case SystemRequestType::BROWSE_FOR_FILE: { @@ -545,28 +534,26 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string return false; } - g_dialogThread = std::thread([=] { + std::thread([=] { std::string out; if (W32Util::BrowseForFileName(true, MainWindow::GetHWND(), ConvertUTF8ToWString(param1).c_str(), nullptr, filter.c_str(), L"", out)) { g_requestManager.PostSystemSuccess(requestId, out.c_str()); } else { g_requestManager.PostSystemFailure(requestId); } - }); - g_dialogThread.detach(); + }).detach(); return true; } case SystemRequestType::BROWSE_FOR_FOLDER: { - g_dialogThread = std::thread([=] { + std::thread([=] { std::string folder = W32Util::BrowseForFolder(MainWindow::GetHWND(), param1.c_str()); if (folder.size()) { g_requestManager.PostSystemSuccess(requestId, folder.c_str()); } else { g_requestManager.PostSystemFailure(requestId); } - }); - g_dialogThread.detach(); + }).detach(); return true; } case SystemRequestType::TOGGLE_FULLSCREEN_STATE: @@ -728,9 +715,8 @@ static void WinMainInit() { } static void WinMainCleanup() { - if (g_dialogThread.joinable()) { - g_dialogThread.join(); - } + // This will ensure no further callbacks are called, which may prevent crashing. + g_requestManager.Clear(); net::Shutdown(); CoUninitialize();