From faf24e91ae6c277ee6f8cee6ae8bd77c9db99a87 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 6 Nov 2026 00:31:15 -0800 Subject: [PATCH 1/3] Extract application shutdown without changing startup behavior --- .../Source/CommonFramework/Main.cpp | 78 +++++++++++-------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/SerialPrograms/Source/CommonFramework/Main.cpp b/SerialPrograms/Source/CommonFramework/Main.cpp index 0e7bb7fd3f..2e29324bc5 100644 --- a/SerialPrograms/Source/CommonFramework/Main.cpp +++ b/SerialPrograms/Source/CommonFramework/Main.cpp @@ -280,6 +280,51 @@ int run_program(int argc, char *argv[]){ } +namespace{ + +// Stop GUI services after run_program() returns, preserving the existing order. +void stop_application_services(){ +#ifdef PA_SOCIAL_SDK + Integration::DiscordSocialSDK::DiscordSocial::instance().stop(); +#endif + +#ifdef PA_DPP + Integration::DppClient::Client::instance().stop(); +#endif + + // We must clear the OCR cache or it will crash on Linux when the library + // unloads before the cache is destructed from static memory. + OCR::clear_ocr_cache(); + + // Stop the controllers. + global_input_stop(); + + // Stop misc. services. + Integration::DiscordWebhook::DiscordWebhookSender::instance().stop(); + SystemSleepController::instance().stop(); + global_periodic_runner().stop(); + global_watchdog().stop(); + global_file_logger().stop(); + +} + +// Join the worker pools after service shutdown and the Windows Qt workaround. +void stop_thread_pools(){ + // Force stop the thread pools. + // This is where all the threads in the program are joined. + PokemonAutomation::GlobalThreadPools::computation_realtime().stop(); + PokemonAutomation::GlobalThreadPools::computation_normal().stop(); + PokemonAutomation::GlobalThreadPools::unlimited_realtime().stop(); + PokemonAutomation::GlobalThreadPools::unlimited_normal().stop(); + PokemonAutomation::global_dispatcher.stop(); + + GlobalThreadPools::qt_worker_threadpool().stop(); + GlobalThreadPools::qt_event_threadpool().stop(); + +} + +} // namespace + int main(int argc, char *argv[]){ #if 0 // Retrieve and store program name @@ -303,27 +348,7 @@ int main(int argc, char *argv[]){ int ret = run_program(argc, argv); -#ifdef PA_SOCIAL_SDK - Integration::DiscordSocialSDK::DiscordSocial::instance().stop(); -#endif - -#ifdef PA_DPP - Integration::DppClient::Client::instance().stop(); -#endif - - // We must clear the OCR cache or it will crash on Linux when the library - // unloads before the cache is destructed from static memory. - OCR::clear_ocr_cache(); - - // Stop the controllers. - global_input_stop(); - - // Stop misc. services. - Integration::DiscordWebhook::DiscordWebhookSender::instance().stop(); - SystemSleepController::instance().stop(); - global_periodic_runner().stop(); - global_watchdog().stop(); - global_file_logger().stop(); + stop_application_services(); // // Workaround Qt 6.9 thread-adoption bug on Windows. @@ -337,16 +362,7 @@ int main(int argc, char *argv[]){ std::quick_exit(ret); #endif - // Force stop the thread pools. - // This is where all the threads in the program are joined. - PokemonAutomation::GlobalThreadPools::computation_realtime().stop(); - PokemonAutomation::GlobalThreadPools::computation_normal().stop(); - PokemonAutomation::GlobalThreadPools::unlimited_realtime().stop(); - PokemonAutomation::GlobalThreadPools::unlimited_normal().stop(); - PokemonAutomation::global_dispatcher.stop(); - - GlobalThreadPools::qt_worker_threadpool().stop(); - GlobalThreadPools::qt_event_threadpool().stop(); + stop_thread_pools(); std::cout << "Exiting main()..." << std::endl; From ccb626f1f0ef4e0d1bb039494efdd93268d970f0 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 6 Nov 2026 00:40:26 -0800 Subject: [PATCH 2/3] Group Main.cpp helpers in one anonymous namespace --- SerialPrograms/Source/CommonFramework/Main.cpp | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/SerialPrograms/Source/CommonFramework/Main.cpp b/SerialPrograms/Source/CommonFramework/Main.cpp index 2e29324bc5..3522fdb665 100644 --- a/SerialPrograms/Source/CommonFramework/Main.cpp +++ b/SerialPrograms/Source/CommonFramework/Main.cpp @@ -45,15 +45,17 @@ #include "PersistentSettings.h" #include "NintendoSwitch/NintendoSwitch_SettingsPanel.h" +Q_DECLARE_METATYPE(std::string) + using namespace PokemonAutomation; namespace PokemonAutomation{ - bool use_qt_ui(){ - return true; - } +bool use_qt_ui(){ + return true; } +} // namespace PokemonAutomation -Q_DECLARE_METATYPE(std::string) +namespace{ void set_working_directory(){ const std::string app_dir = Filesystem::application_binary_directory().string_slash_normalized(); @@ -69,10 +71,6 @@ void set_working_directory(){ } } -namespace PokemonAutomation{ - - - FileLogger& global_file_logger(){ static FileLogger logger( GlobalThreadPools::unlimited_normal(), @@ -83,8 +81,6 @@ FileLogger& global_file_logger(){ return logger; } -} - int run_program(int argc, char *argv[]){ @@ -280,8 +276,6 @@ int run_program(int argc, char *argv[]){ } -namespace{ - // Stop GUI services after run_program() returns, preserving the existing order. void stop_application_services(){ #ifdef PA_SOCIAL_SDK From a380f2cb34b5e59937d36730d482334b88bd786a Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 6 Nov 2026 00:47:15 -0800 Subject: [PATCH 3/3] Nest file-local helpers in PokemonAutomation without a using directive --- SerialPrograms/Source/CommonFramework/Main.cpp | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/SerialPrograms/Source/CommonFramework/Main.cpp b/SerialPrograms/Source/CommonFramework/Main.cpp index 3522fdb665..50018b7585 100644 --- a/SerialPrograms/Source/CommonFramework/Main.cpp +++ b/SerialPrograms/Source/CommonFramework/Main.cpp @@ -47,13 +47,10 @@ Q_DECLARE_METATYPE(std::string) -using namespace PokemonAutomation; - namespace PokemonAutomation{ bool use_qt_ui(){ return true; } -} // namespace PokemonAutomation namespace{ @@ -318,6 +315,7 @@ void stop_thread_pools(){ } } // namespace +} // namespace PokemonAutomation int main(int argc, char *argv[]){ #if 0 @@ -337,12 +335,12 @@ int main(int argc, char *argv[]){ // So far, this is only needed on Mac where static initialization is fucked up. PokemonAutomation::register_all_statics(); - setup_crash_handler(); + PokemonAutomation::setup_crash_handler(); - int ret = run_program(argc, argv); + int ret = PokemonAutomation::run_program(argc, argv); - stop_application_services(); + PokemonAutomation::stop_application_services(); // // Workaround Qt 6.9 thread-adoption bug on Windows. @@ -356,7 +354,7 @@ int main(int argc, char *argv[]){ std::quick_exit(ret); #endif - stop_thread_pools(); + PokemonAutomation::stop_thread_pools(); std::cout << "Exiting main()..." << std::endl;