1
0

Fixed crash on exit introduced with Windows Service capability.

Ref.: #1845
This commit is contained in:
Mattes D 2015-04-05 17:07:10 +02:00
parent 773ac22c30
commit 31953b19b8
3 changed files with 29 additions and 31 deletions

View File

@ -34,6 +34,7 @@
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_WebAdmin(nullptr),
m_PluginManager(nullptr), m_PluginManager(nullptr),
m_MojangAPI(nullptr), m_MojangAPI(nullptr),
m_bStop(false),
m_bRestart(false) m_bRestart(false)
{ {
s_Root = this; s_Root = this;
@ -71,7 +71,7 @@ void cRoot::InputThread(cRoot & a_Params)
{ {
cLogCommandOutputCallback Output; 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; AString Command;
std::getline(std::cin, Command); std::getline(std::cin, Command);
@ -83,10 +83,11 @@ void cRoot::InputThread(cRoot & a_Params)
if (m_TerminateEventRaised || !std::cin.good()) 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: // We have come here because the std::cin has received an EOF / a terminate signal has been sent, and the server is still running
if (m_RunAsService) // HACK: Dont kill if running as a service // 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; cDeadlockDetect dd;
m_bStop = false; m_ShouldStop = false;
while (!m_bStop) while (!m_ShouldStop)
{ {
auto BeginTime = std::chrono::steady_clock::now(); auto BeginTime = std::chrono::steady_clock::now();
m_bRestart = false; m_bRestart = false;
@ -206,14 +207,14 @@ void cRoot::Start(void)
EnableMenuItem(hmenu, SC_CLOSE, MF_ENABLED); // Re-enable close button EnableMenuItem(hmenu, SC_CLOSE, MF_ENABLED); // Re-enable close button
#endif #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)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }
if (m_TerminateEventRaised) if (m_TerminateEventRaised)
{ {
m_bStop = true; m_ShouldStop = true;
} }
// Stop the server: // Stop the server:
@ -224,7 +225,7 @@ void cRoot::Start(void)
} // if (m_Server->Start()) } // if (m_Server->Start())
else else
{ {
m_bStop = true; m_ShouldStop = true;
} }
delete m_MojangAPI; m_MojangAPI = nullptr; 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() void cRoot::LoadGlobalSettings()
{ {
@ -466,7 +460,7 @@ void cRoot::QueueExecuteConsoleCommand(const AString & a_Cmd, cCommandOutputCall
// Some commands are built-in: // Some commands are built-in:
if (a_Cmd == "stop") if (a_Cmd == "stop")
{ {
m_bStop = true; m_ShouldStop = true;
} }
else if (a_Cmd == "restart") 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 // cRoot handles stopping and restarting due to our access to controlling variables
if (a_Cmd == "stop") if (a_Cmd == "stop")
{ {
m_bStop = true; m_ShouldStop = true;
return; return;
} }
else if (a_Cmd == "restart") else if (a_Cmd == "restart")

View File

@ -47,6 +47,7 @@ public:
static bool m_TerminateEventRaised; static bool m_TerminateEventRaised;
static bool m_RunAsService; static bool m_RunAsService;
static bool m_ShouldStop;
cRoot(void); cRoot(void);
@ -54,9 +55,6 @@ public:
void Start(void); void Start(void);
// Added so the service handler can request a stop
void SetStopping(bool a_Stopping);
// tolua_begin // tolua_begin
cServer * GetServer(void) { return m_Server; } cServer * GetServer(void) { return m_Server; }
cWorld * GetDefaultWorld(void); cWorld * GetDefaultWorld(void);
@ -201,7 +199,6 @@ private:
cHTTPServer m_HTTPServer; cHTTPServer m_HTTPServer;
bool m_bStop;
bool m_bRestart; bool m_bRestart;
void LoadGlobalSettings(); void LoadGlobalSettings();

View File

@ -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 */ /** If something has told the server to stop; checked periodically in cRoot */
@ -35,6 +33,9 @@ bool g_ShouldLogCommOut;
bool cRoot::m_RunAsService = false; bool cRoot::m_RunAsService = false;
#if defined(_WIN32) #if defined(_WIN32)
SERVICE_STATUS_HANDLE g_StatusHandle = NULL; SERVICE_STATUS_HANDLE g_StatusHandle = NULL;
HANDLE g_ServiceThread = INVALID_HANDLE_VALUE; HANDLE g_ServiceThread = INVALID_HANDLE_VALUE;
@ -42,7 +43,10 @@ HANDLE g_ServiceThread = INVALID_HANDLE_VALUE;
#endif #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 // _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. // and we haven't had a memory leak for over a year anyway.
// #define ENABLE_LEAK_FINDER // #define ENABLE_LEAK_FINDER
@ -169,6 +173,7 @@ LONG WINAPI LastChanceExceptionFilter(__in struct _EXCEPTION_POINTERS * a_Except
#ifdef _WIN32 #ifdef _WIN32
// Handle CTRL events in windows, including console window close // Handle CTRL events in windows, including console window close
BOOL CtrlHandler(DWORD fdwCtrlType) BOOL CtrlHandler(DWORD fdwCtrlType)
@ -188,6 +193,7 @@ BOOL CtrlHandler(DWORD fdwCtrlType)
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// universalMain - Main startup logic for both standard running and as a service // universalMain - Main startup logic for both standard running and as a service
@ -210,6 +216,7 @@ void universalMain()
try try
#endif #endif
{ {
cRoot Root;
Root.Start(); Root.Start();
} }
#if !defined(ANDROID_NDK) #if !defined(ANDROID_NDK)
@ -282,7 +289,7 @@ void WINAPI serviceCtrlHandler(DWORD CtrlCode)
{ {
case SERVICE_CONTROL_STOP: case SERVICE_CONTROL_STOP:
{ {
Root.SetStopping(true); cRoot::m_ShouldStop = true;
serviceSetState(0, SERVICE_STOP_PENDING, 0); serviceSetState(0, SERVICE_STOP_PENDING, 0);
break; break;
} }