From 31953b19b805facde2114855563540a81bf9bdd8 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Sun, 5 Apr 2015 17:07:10 +0200 Subject: Fixed crash on exit introduced with Windows Service capability. Ref.: #1845 --- src/Root.cpp | 34 ++++++++++++++-------------------- src/Root.h | 5 +---- src/main.cpp | 21 ++++++++++++++------- 3 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/Root.cpp b/src/Root.cpp index 87bc29627..690bd7357 100644 --- a/src/Root.cpp +++ b/src/Root.cpp @@ -33,7 +33,8 @@ -cRoot* cRoot::s_Root = nullptr; +cRoot * cRoot::s_Root = nullptr; +bool cRoot::m_ShouldStop = false; @@ -48,7 +49,6 @@ cRoot::cRoot(void) : m_WebAdmin(nullptr), m_PluginManager(nullptr), m_MojangAPI(nullptr), - m_bStop(false), m_bRestart(false) { s_Root = this; @@ -71,7 +71,7 @@ void cRoot::InputThread(cRoot & a_Params) { cLogCommandOutputCallback Output; - while (!a_Params.m_bStop && !a_Params.m_bRestart && !m_TerminateEventRaised && std::cin.good()) + while (!cRoot::m_ShouldStop && !a_Params.m_bRestart && !m_TerminateEventRaised && std::cin.good()) { AString Command; std::getline(std::cin, Command); @@ -83,10 +83,11 @@ void cRoot::InputThread(cRoot & a_Params) if (m_TerminateEventRaised || !std::cin.good()) { - // We have come here because the std::cin has received an EOF / a terminate signal has been sent, and the server is still running; stop the server: - if (m_RunAsService) // HACK: Dont kill if running as a service + // We have come here because the std::cin has received an EOF / a terminate signal has been sent, and the server is still running + // Stop the server: + if (!m_RunAsService) // Dont kill if running as a service { - a_Params.m_bStop = true; + a_Params.m_ShouldStop = true; } } } @@ -117,8 +118,8 @@ void cRoot::Start(void) cDeadlockDetect dd; - m_bStop = false; - while (!m_bStop) + m_ShouldStop = false; + while (!m_ShouldStop) { auto BeginTime = std::chrono::steady_clock::now(); m_bRestart = false; @@ -206,14 +207,14 @@ void cRoot::Start(void) EnableMenuItem(hmenu, SC_CLOSE, MF_ENABLED); // Re-enable close button #endif - while (!m_bStop && !m_bRestart && !m_TerminateEventRaised) // These are modified by external threads + while (!m_ShouldStop && !m_bRestart && !m_TerminateEventRaised) // These are modified by external threads { std::this_thread::sleep_for(std::chrono::seconds(1)); } if (m_TerminateEventRaised) { - m_bStop = true; + m_ShouldStop = true; } // Stop the server: @@ -224,7 +225,7 @@ void cRoot::Start(void) } // if (m_Server->Start()) else { - m_bStop = true; + m_ShouldStop = true; } delete m_MojangAPI; m_MojangAPI = nullptr; @@ -271,13 +272,6 @@ void cRoot::Start(void) -void cRoot::SetStopping(bool a_Stopping) -{ - m_bStop = a_Stopping; -} - - - void cRoot::LoadGlobalSettings() { @@ -466,7 +460,7 @@ void cRoot::QueueExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCall // Some commands are built-in: if (a_Cmd == "stop") { - m_bStop = true; + m_ShouldStop = true; } else if (a_Cmd == "restart") { @@ -498,7 +492,7 @@ void cRoot::ExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCallback // cRoot handles stopping and restarting due to our access to controlling variables if (a_Cmd == "stop") { - m_bStop = true; + m_ShouldStop = true; return; } else if (a_Cmd == "restart") diff --git a/src/Root.h b/src/Root.h index d552466dc..2f9d1eb2c 100644 --- a/src/Root.h +++ b/src/Root.h @@ -47,6 +47,7 @@ public: static bool m_TerminateEventRaised; static bool m_RunAsService; + static bool m_ShouldStop; cRoot(void); @@ -54,9 +55,6 @@ public: void Start(void); - // Added so the service handler can request a stop - void SetStopping(bool a_Stopping); - // tolua_begin cServer * GetServer(void) { return m_Server; } cWorld * GetDefaultWorld(void); @@ -201,7 +199,6 @@ private: cHTTPServer m_HTTPServer; - bool m_bStop; bool m_bRestart; void LoadGlobalSettings(); diff --git a/src/main.cpp b/src/main.cpp index da8eb75d4..80e457b54 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,8 +15,6 @@ -/** Make the Root instance global, so it can be terminated from the worker threads */ -cRoot Root; /** If something has told the server to stop; checked periodically in cRoot */ @@ -35,14 +33,20 @@ bool g_ShouldLogCommOut; bool cRoot::m_RunAsService = false; + + + #if defined(_WIN32) -SERVICE_STATUS_HANDLE g_StatusHandle = NULL; -HANDLE g_ServiceThread = INVALID_HANDLE_VALUE; -#define SERVICE_NAME "MCServerService" + SERVICE_STATUS_HANDLE g_StatusHandle = NULL; + HANDLE g_ServiceThread = INVALID_HANDLE_VALUE; + #define SERVICE_NAME "MCServerService" #endif -/// If defined, a thorough leak finder will be used (debug MSVC only); leaks will be output to the Output window + + + +/** If defined, a thorough leak finder will be used (debug MSVC only); leaks will be output to the Output window */ // _X 2014_02_20: Disabled for canon repo, it makes the debug version too slow in MSVC2013 // and we haven't had a memory leak for over a year anyway. // #define ENABLE_LEAK_FINDER @@ -169,6 +173,7 @@ LONG WINAPI LastChanceExceptionFilter(__in struct _EXCEPTION_POINTERS * a_Except + #ifdef _WIN32 // Handle CTRL events in windows, including console window close BOOL CtrlHandler(DWORD fdwCtrlType) @@ -188,6 +193,7 @@ BOOL CtrlHandler(DWORD fdwCtrlType) + //////////////////////////////////////////////////////////////////////////////// // universalMain - Main startup logic for both standard running and as a service @@ -210,6 +216,7 @@ void universalMain() try #endif { + cRoot Root; Root.Start(); } #if !defined(ANDROID_NDK) @@ -282,7 +289,7 @@ void WINAPI serviceCtrlHandler(DWORD CtrlCode) { case SERVICE_CONTROL_STOP: { - Root.SetStopping(true); + cRoot::m_ShouldStop = true; serviceSetState(0, SERVICE_STOP_PENDING, 0); break; } -- cgit v1.2.3