From 4647497ceea2316ba4e1b94c43e6ff4f9b68afa0 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 17 Jun 2010 23:24:15 +0000 Subject: [PATCH] - bugfix for proper thread shutdown which caused a number of crashes when navigating menus --- source/glest_game/facilities/game_util.cpp | 2 +- .../menu/menu_state_custom_game.cpp | 12 ++--- .../menu/menu_state_masterserver.cpp | 46 +++++++++++++++++-- source/glest_game/network/connection_slot.cpp | 15 ------ .../include/platform/common/base_thread.h | 2 +- 5 files changed, 47 insertions(+), 30 deletions(-) diff --git a/source/glest_game/facilities/game_util.cpp b/source/glest_game/facilities/game_util.cpp index f08703096..92f2ce408 100644 --- a/source/glest_game/facilities/game_util.cpp +++ b/source/glest_game/facilities/game_util.cpp @@ -26,7 +26,7 @@ using namespace Shared::Platform; namespace Glest{ namespace Game{ const string mailString= "contact_game@glest.org"; -const string glestVersionString= "v3.3.5-beta7.3"; +const string glestVersionString= "v3.3.5-beta7.4"; string getCrashDumpFileName(){ return "glest" + glestVersionString + ".dmp"; diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index f6b7dd2da..ea9a32576 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -326,11 +326,6 @@ MenuStateCustomGame::~MenuStateCustomGame() { needToRepublishToMasterserver = false; BaseThread::shutdownAndWait(publishToMasterserverThread); - safeMutex.ReleaseLock(true); - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - - safeMutex.Lock(); delete publishToMasterserverThread; publishToMasterserverThread = NULL; safeMutex.ReleaseLock(); @@ -346,6 +341,8 @@ void MenuStateCustomGame::returnToParentMenu(){ needToRepublishToMasterserver = false; BaseThread::shutdownAndWait(publishToMasterserverThread); + delete publishToMasterserverThread; + publishToMasterserverThread = NULL; SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -389,6 +386,8 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ needToBroadcastServerSettings = false; needToRepublishToMasterserver = false; BaseThread::shutdownAndWait(publishToMasterserverThread); + delete publishToMasterserverThread; + publishToMasterserverThread = NULL; safeMutex.ReleaseLock(); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -442,9 +441,6 @@ void MenuStateCustomGame::mouseClick(int x, int y, MouseButton mouseButton){ needToRepublishToMasterserver = false; BaseThread::shutdownAndWait(publishToMasterserverThread); - - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); - delete publishToMasterserverThread; publishToMasterserverThread = NULL; safeMutex.ReleaseLock(); diff --git a/source/glest_game/menu/menu_state_masterserver.cpp b/source/glest_game/menu/menu_state_masterserver.cpp index cd498403e..25f3e4fbc 100644 --- a/source/glest_game/menu/menu_state_masterserver.cpp +++ b/source/glest_game/menu/menu_state_masterserver.cpp @@ -132,6 +132,8 @@ void ServerLine::render(){ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMenu): MenuState(program, mainMenu, "masterserver") { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + Lang &lang= Lang::getInstance(); autoRefreshTime=0; @@ -169,6 +171,8 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen listBoxAutoRefresh.pushBackItem("30 s"); listBoxAutoRefresh.setSelectedItemIndex(0); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + NetworkManager::getInstance().end(); NetworkManager::getInstance().init(nrClient); //updateServerInfo(); @@ -176,17 +180,25 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen needUpdateFromServer = true; updateFromMasterserverThread = new SimpleTaskThread(this,0,100); updateFromMasterserverThread->start(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } MenuStateMasterserver::~MenuStateMasterserver() { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + MutexSafeWrapper safeMutex(&masterServerThreadAccessor); + needUpdateFromServer = false; clearServerLines(); + safeMutex.ReleaseLock(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); BaseThread::shutdownAndWait(updateFromMasterserverThread); delete updateFromMasterserverThread; updateFromMasterserverThread = NULL; - safeMutex.ReleaseLock(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } void MenuStateMasterserver::clearServerLines(){ @@ -197,6 +209,7 @@ void MenuStateMasterserver::clearServerLines(){ } void MenuStateMasterserver::mouseClick(int x, int y, MouseButton mouseButton){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); CoreData &coreData= CoreData::getInstance(); SoundRenderer &soundRenderer= SoundRenderer::getInstance(); @@ -213,27 +226,49 @@ void MenuStateMasterserver::mouseClick(int x, int y, MouseButton mouseButton){ } } else if(buttonRefresh.mouseClick(x, y)){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + MutexSafeWrapper safeMutex(&masterServerThreadAccessor); soundRenderer.playFx(coreData.getClickSoundB()); //updateServerInfo(); needUpdateFromServer = true; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } else if(buttonReturn.mouseClick(x, y)){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + MutexSafeWrapper safeMutex(&masterServerThreadAccessor); soundRenderer.playFx(coreData.getClickSoundB()); BaseThread::shutdownAndWait(updateFromMasterserverThread); + delete updateFromMasterserverThread; + updateFromMasterserverThread = NULL; + safeMutex.ReleaseLock(); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + mainMenu->setState(new MenuStateRoot(program, mainMenu)); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } else if(buttonCreateGame.mouseClick(x, y)){ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + MutexSafeWrapper safeMutex(&masterServerThreadAccessor); soundRenderer.playFx(coreData.getClickSoundB()); - + needUpdateFromServer = false; BaseThread::shutdownAndWait(updateFromMasterserverThread); + delete updateFromMasterserverThread; + updateFromMasterserverThread = NULL; safeMutex.ReleaseLock(); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + mainMenu->setState(new MenuStateCustomGame(program, mainMenu,true,true)); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } else if(listBoxAutoRefresh.mouseClick(x, y)){ MutexSafeWrapper safeMutex(&masterServerThreadAccessor); @@ -295,8 +330,8 @@ void MenuStateMasterserver::render(){ void MenuStateMasterserver::update(){ MutexSafeWrapper safeMutex(&masterServerThreadAccessor); if(autoRefreshTime!=0 && difftime(time(NULL),lastRefreshTimer) >= autoRefreshTime ) { - needUpdateFromServer = true; - lastRefreshTimer= time(NULL); + needUpdateFromServer = true; + lastRefreshTimer= time(NULL); } if(playServerFoundSound) @@ -426,7 +461,8 @@ void MenuStateMasterserver::connectToServer(string ipString) //config.save(); BaseThread::shutdownAndWait(updateFromMasterserverThread); - + delete updateFromMasterserverThread; + updateFromMasterserverThread = NULL; safeMutex.ReleaseLock(); mainMenu->setState(new MenuStateConnectedGame(program, mainMenu,jmMasterserver)); diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index ccfae152c..5690c53d2 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -508,27 +508,12 @@ void ConnectionSlot::update(bool checkForNewClients) { void ConnectionSlot::close() { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s LINE: %d]\n",__FILE__,__FUNCTION__,__LINE__); - // In case we are closing from within the context of the thread - // only signal it to quit here - //if(slotThreadWorker != NULL) { - // slotThreadWorker->signalQuit(); - //} - //BaseThread::shutdownAndWait(slotThreadWorker); - //delete slotThreadWorker; - //slotThreadWorker = NULL; - - SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s LINE: %d]\n",__FILE__,__FUNCTION__,__LINE__); - bool updateServerListener = (socket != NULL); delete socket; socket= NULL; SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s LINE: %d]\n",__FILE__,__FUNCTION__,__LINE__); - //chatText.clear(); - //chatSender.clear(); - //chatTeamIndex= -1; - if(updateServerListener == true && ready == false) { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s LINE: %d]\n",__FILE__,__FUNCTION__,__LINE__); serverInterface->updateListen(); diff --git a/source/shared_lib/include/platform/common/base_thread.h b/source/shared_lib/include/platform/common/base_thread.h index 0575436ab..233dec440 100644 --- a/source/shared_lib/include/platform/common/base_thread.h +++ b/source/shared_lib/include/platform/common/base_thread.h @@ -36,7 +36,7 @@ protected: public: BaseThread(); - ~BaseThread(); + virtual ~BaseThread(); virtual void execute()=0; virtual void signalQuit();