diff --git a/source/glest_game/ai/ai_interface.cpp b/source/glest_game/ai/ai_interface.cpp index 2801946b..ceb5fbdf 100644 --- a/source/glest_game/ai/ai_interface.cpp +++ b/source/glest_game/ai/ai_interface.cpp @@ -234,8 +234,9 @@ AiInterface::AiInterface(Game &game, int factionIndex, int teamIndex, } workerThread = NULL; } + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); this->workerThread = new AiInterfaceThread(this); - this->workerThread->setUniqueID(__FILE__); + this->workerThread->setUniqueID(mutexOwnerId); this->workerThread->start(); } diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index e5be01d2..f3bb7435 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -257,8 +257,9 @@ Renderer::Renderer() : BaseRenderer() { } if(GlobalStaticFlags::getIsNonGraphicalModeEnabled() == false) { + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); saveScreenShotThread = new SimpleTaskThread(this,0,25); - saveScreenShotThread->setUniqueID(extractFileFromDirectoryPath(__FILE__).c_str()); + saveScreenShotThread->setUniqueID(mutexOwnerId); saveScreenShotThread->start(); } } @@ -266,20 +267,27 @@ Renderer::Renderer() : BaseRenderer() { void Renderer::cleanupScreenshotThread() { if(saveScreenShotThread) { saveScreenShotThread->signalQuit(); - for(time_t elapsed = time(NULL); - getSaveScreenQueueSize() > 0 && difftime((long int)time(NULL),elapsed) <= 7;) { - sleep(0); - } - if(saveScreenShotThread->canShutdown(true) == true && - saveScreenShotThread->shutdownAndWait() == true) { - //printf("IN MenuStateCustomGame cleanup - C\n"); +// for(time_t elapsed = time(NULL); +// getSaveScreenQueueSize() > 0 && difftime((long int)time(NULL),elapsed) <= 7;) { +// sleep(0); +// } +// if(saveScreenShotThread->canShutdown(true) == true && +// saveScreenShotThread->shutdownAndWait() == true) { +// //printf("IN MenuStateCustomGame cleanup - C\n"); +// delete saveScreenShotThread; +// } +// saveScreenShotThread = NULL; + if(saveScreenShotThread->shutdownAndWait() == true) { delete saveScreenShotThread; } saveScreenShotThread = NULL; + if(getSaveScreenQueueSize() > 0) { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] FORCING MEMORY CLEANUP and NOT SAVING screenshots, saveScreenQueue.size() = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,saveScreenQueue.size()); + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); + MutexSafeWrapper safeMutex(&saveScreenShotThreadAccessor,mutexOwnerId); for(std::list >::iterator iter = saveScreenQueue.begin(); iter != saveScreenQueue.end(); ++iter) { delete iter->second; @@ -290,37 +298,57 @@ void Renderer::cleanupScreenshotThread() { } Renderer::~Renderer() { - delete modelRenderer; - modelRenderer = NULL; - delete textRenderer; - textRenderer = NULL; - delete textRenderer3D; - textRenderer3D = NULL; - delete particleRenderer; - particleRenderer = NULL; + try{ + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); - //resources - for(int i=0; imenu = NULL; + this->game = NULL; + this->gameCamera = NULL; } + catch(const exception &e) { + char szBuf[8096]=""; + snprintf(szBuf,8096,"In [%s::%s Line: %d]\nError [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,e.what()); + SystemFlags::OutputDebug(SystemFlags::debugError,szBuf); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,szBuf); - // Wait for the queue to become empty or timeout the thread at 7 seconds - cleanupScreenshotThread(); - - mapSurfaceData.clear(); - quadCache = VisibleQuadContainerCache(); - quadCache.clearFrustrumData(); - - this->menu = NULL; - this->game = NULL; - this->gameCamera = NULL; + throw megaglest_runtime_error(szBuf); + } } void Renderer::simpleTask(BaseThread *callingThread) { diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index c339637d..eacf7782 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -4842,8 +4842,9 @@ int glestMain(int argc, char** argv) { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d] precache thread enabled = %d\n",__FILE__,__FUNCTION__,__LINE__,startCRCPrecacheThread); if(startCRCPrecacheThread == true) { vector techDataPaths = config.getPathListForType(ptTechs); + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); preCacheThread = new FileCRCPreCacheThread(); - preCacheThread->setUniqueID(__FILE__); + preCacheThread->setUniqueID(mutexOwnerId); preCacheThread->setTechDataPaths(techDataPaths); //preCacheThread->setFileCRCPreCacheThreadCallbackInterface(&preCacheThreadGame); preCacheThread->start(); diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index 2d51e39c..0ac9e912 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -807,8 +807,9 @@ void Program::init(WindowGl *window, bool initSound, bool toggleFullScreen){ if(BaseThread::shutdownAndWait(soundThreadManager) == true) { delete soundThreadManager; } + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,SOUND_THREAD_UPDATE_MILLISECONDS); - soundThreadManager->setUniqueID(extractFileFromDirectoryPath(__FILE__).c_str()); + soundThreadManager->setUniqueID(mutexOwnerId); soundThreadManager->start(); } } @@ -886,8 +887,9 @@ void Program::stopSoundSystem() { void Program::startSoundSystem() { stopSoundSystem(); if(SoundRenderer::getInstance().runningThreaded() == true) { + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); soundThreadManager = new SimpleTaskThread(&SoundRenderer::getInstance(),0,SOUND_THREAD_UPDATE_MILLISECONDS); - soundThreadManager->setUniqueID(extractFileFromDirectoryPath(__FILE__).c_str()); + soundThreadManager->setUniqueID(mutexOwnerId); soundThreadManager->start(); } } diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index 392f7e68..08a13ec8 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -544,8 +544,9 @@ MenuStateConnectedGame::MenuStateConnectedGame(Program *program, MainMenu *mainM ftpClientThread->start(); } // Start http meta data thread + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); modHttpServerThread = new SimpleTaskThread(this,0,200); - modHttpServerThread->setUniqueID(__FILE__); + modHttpServerThread->setUniqueID(mutexOwnerId); modHttpServerThread->start(); if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 0d7c0011..fce282d7 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -705,8 +705,9 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, GraphicComponent::applyAllCustomProperties(containerName); + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); publishToMasterserverThread = new SimpleTaskThread(this,0,200); - publishToMasterserverThread->setUniqueID(__FILE__); + publishToMasterserverThread->setUniqueID(mutexOwnerId); publishToMasterserverThread->start(); if(openNetworkSlots==true){ diff --git a/source/glest_game/menu/menu_state_masterserver.cpp b/source/glest_game/menu/menu_state_masterserver.cpp index 8418631e..cef9d1f2 100644 --- a/source/glest_game/menu/menu_state_masterserver.cpp +++ b/source/glest_game/menu/menu_state_masterserver.cpp @@ -270,8 +270,10 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen chatManager.setFont3D(CoreData::getInstance().getMenuFontNormal3D()); needUpdateFromServer = true; + + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); updateFromMasterserverThread = new SimpleTaskThread(this,0,100); - updateFromMasterserverThread->setUniqueID(extractFileFromDirectoryPath(__FILE__).c_str()); + updateFromMasterserverThread->setUniqueID(mutexOwnerId); updateFromMasterserverThread->start(); if(Config::getInstance().getString("IRCServer","") != "") { diff --git a/source/glest_game/menu/menu_state_mods.cpp b/source/glest_game/menu/menu_state_mods.cpp index 4955dd2b..bdf6314f 100644 --- a/source/glest_game/menu/menu_state_mods.cpp +++ b/source/glest_game/menu/menu_state_mods.cpp @@ -250,8 +250,9 @@ MenuStateMods::MenuStateMods(Program *program, MainMenu *mainMenu) : if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); // Start http meta data thread + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); modHttpServerThread = new SimpleTaskThread(this,0,200); - modHttpServerThread->setUniqueID(__FILE__); + modHttpServerThread->setUniqueID(mutexOwnerId); modHttpServerThread->start(); if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/network/client_interface.cpp b/source/glest_game/network/client_interface.cpp index f2ccfe43..776fa541 100644 --- a/source/glest_game/network/client_interface.cpp +++ b/source/glest_game/network/client_interface.cpp @@ -934,8 +934,9 @@ void ClientInterface::updateKeyframe(int frameCount) { } else { if(networkCommandListThread == NULL) { + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); networkCommandListThread = new SimpleTaskThread(this,0,0); - networkCommandListThread->setUniqueID(extractFileFromDirectoryPath(__FILE__).c_str()); + networkCommandListThread->setUniqueID(mutexOwnerId); networkCommandListThread->start(); } diff --git a/source/glest_game/network/connection_slot.cpp b/source/glest_game/network/connection_slot.cpp index 2145d7f0..2f48717f 100644 --- a/source/glest_game/network/connection_slot.cpp +++ b/source/glest_game/network/connection_slot.cpp @@ -275,8 +275,9 @@ ConnectionSlot::ConnectionSlot(ServerInterface* serverInterface, int playerIndex this->setSocket(NULL); this->slotThreadWorker = NULL; + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); this->slotThreadWorker = new ConnectionSlotThread(this->serverInterface,playerIndex); - this->slotThreadWorker->setUniqueID(__FILE__); + this->slotThreadWorker->setUniqueID(mutexOwnerId); this->slotThreadWorker->start(); this->ready = false; diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index 589a1912..dcb0f009 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -205,8 +205,9 @@ ServerInterface::ServerInterface(bool publishEnabled) :GameNetworkInterface() { if(publishToMasterserverThread == NULL) { if(needToRepublishToMasterserver == true || GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); publishToMasterserverThread = new SimpleTaskThread(this,0,125); - publishToMasterserverThread->setUniqueID(extractFileFromDirectoryPath(__FILE__).c_str()); + publishToMasterserverThread->setUniqueID(mutexOwnerId); publishToMasterserverThread->start(); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,needToRepublishToMasterserver); @@ -2135,8 +2136,9 @@ bool ServerInterface::launchGame(const GameSettings *gameSettings) { if(publishToMasterserverThread == NULL) { if(needToRepublishToMasterserver == true || GlobalStaticFlags::getIsNonGraphicalModeEnabled() == true) { + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); publishToMasterserverThread = new SimpleTaskThread(this,0,125); - publishToMasterserverThread->setUniqueID(extractFileFromDirectoryPath(__FILE__).c_str()); + publishToMasterserverThread->setUniqueID(mutexOwnerId); publishToMasterserverThread->start(); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] needToRepublishToMasterserver = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,needToRepublishToMasterserver); diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index 5e02f6ae..e7bd6208 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -614,8 +614,9 @@ void Faction::init( } workerThread = NULL; } + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); this->workerThread = new FactionThread(this); - this->workerThread->setUniqueID(__FILE__); + this->workerThread->setUniqueID(mutexOwnerId); this->workerThread->start(); } diff --git a/source/shared_lib/sources/platform/common/base_thread.cpp b/source/shared_lib/sources/platform/common/base_thread.cpp index c5b6342a..d6233614 100644 --- a/source/shared_lib/sources/platform/common/base_thread.cpp +++ b/source/shared_lib/sources/platform/common/base_thread.cpp @@ -50,16 +50,26 @@ BaseThread::~BaseThread() { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s] ret [%d] END\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str(),ret); MutexSafeWrapper safeMutexMasterList(&mutexMasterThreadList); + + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s] ret [%d] END\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str(),ret); + if(masterThreadList.find(this) == masterThreadList.end()) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s] ret [%d] END\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str(),ret); + char szBuf[8096]=""; snprintf(szBuf,8096,"invalid thread delete for ptr: %p",this); throw megaglest_runtime_error(szBuf); } + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s] ret [%d] END\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str(),ret); + masterThreadList[this]--; if(masterThreadList[this] <= 0) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s] ret [%d] END\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str(),ret); masterThreadList.erase(this); } + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s] ret [%d] END\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str(),ret); safeMutexMasterList.ReleaseLock(); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s] ret [%d] END\n",__FILE__,__FUNCTION__,__LINE__,uniqueID.c_str(),ret); } bool BaseThread::isThreadDeleted(void *ptr) { diff --git a/source/shared_lib/sources/platform/common/simple_threads.cpp b/source/shared_lib/sources/platform/common/simple_threads.cpp index 071126b8..ffc3d93e 100644 --- a/source/shared_lib/sources/platform/common/simple_threads.cpp +++ b/source/shared_lib/sources/platform/common/simple_threads.cpp @@ -134,7 +134,8 @@ void FileCRCPreCacheThread::execute() { new FileCRCPreCacheThread(techDataPaths, workerTechList, this->processTechCB); - workerThread->setUniqueID(__FILE__); + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); + workerThread->setUniqueID(mutexOwnerId); preCacheWorkerThreadList.push_back(workerThread); workerThread->start(); @@ -368,8 +369,8 @@ SimpleTaskThread::~SimpleTaskThread() { SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s Line: %d] Error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] uniqueID [%s]\n",__FILE__,__FUNCTION__,__LINE__,this->getUniqueID().c_str()); - //throw megaglest_runtime_error(ex.what()); - abort(); + throw megaglest_runtime_error(ex.what()); + //abort(); } } diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index 44a6f7ac..a45cb777 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -1791,8 +1791,9 @@ void ClientSocket::startBroadCastClientThread(DiscoveredServersInterface *cb) { ClientSocket::stopBroadCastClientThread(); + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); broadCastClientThread = new BroadCastClientSocketThread(cb); - broadCastClientThread->setUniqueID(string(__FILE__) + string("_") + string(__FUNCTION__)); + broadCastClientThread->setUniqueID(mutexOwnerId); broadCastClientThread->start(); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -2151,7 +2152,8 @@ void ServerSocket::startBroadCastThread() { //printf("Start broadcast thread [%p]\n",broadCastThread); - broadCastThread->setUniqueID(string(__FILE__) + string("_") + string(__FUNCTION__)); + static string mutexOwnerId = string(extractFileFromDirectoryPath(__FILE__).c_str()) + string("_") + intToStr(__LINE__); + broadCastThread->setUniqueID(mutexOwnerId); broadCastThread->start(); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);