From 43ed4553eab3a0e49852683c527f8ec7b20cb972 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Wed, 28 Sep 2011 15:32:57 +0000 Subject: [PATCH] - more memory cleanup and thread cleanup (found using valgrind) --- source/glest_game/main/main.cpp | 7 ++++++ .../menu/menu_state_custom_game.cpp | 4 +++- source/glest_game/network/network_message.cpp | 12 +++++++++- .../shared_lib/include/platform/sdl/thread.h | 10 ++++++++- .../sources/platform/sdl/thread.cpp | 22 ++++++++++++++++++- source/shared_lib/sources/util/util.cpp | 20 ++++++++--------- source/shared_lib/sources/xml/xml_parser.cpp | 2 ++ 7 files changed, 63 insertions(+), 14 deletions(-) diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index f45c1b80..7a9d3423 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -160,6 +160,13 @@ static void cleanupProcessObjects() { //deleteMapValues(crcFactionPreviewTextureCache.begin(),crcFactionPreviewTextureCache.end()); crcFactionPreviewTextureCache.clear(); + time_t elapsed = time(NULL); + for(;Thread::getThreadList().size() > 0 && + difftime(time(NULL),elapsed) <= 10;) { + sleep(0); + } + + SystemFlags::globalCleanupHTTP(); CacheManager::cleanupMutexes(); 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 b47aa5a9..cd870ec8 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -643,7 +643,9 @@ void MenuStateCustomGame::cleanup() { } publishToMasterserverThread = NULL; } - else if(publishToMasterserverThread->canShutdown(true) == true && + else { + publishToMasterserverThread->signalQuit(); + if(publishToMasterserverThread->canShutdown(true) == true && publishToMasterserverThread->shutdownAndWait() == true) { delete publishToMasterserverThread; } diff --git a/source/glest_game/network/network_message.cpp b/source/glest_game/network/network_message.cpp index 2fe76193..5d3297c6 100644 --- a/source/glest_game/network/network_message.cpp +++ b/source/glest_game/network/network_message.cpp @@ -270,7 +270,7 @@ NetworkMessageLaunch::NetworkMessageLaunch(const GameSettings *gameSettings,int8 data.pathFinderType = gameSettings->getPathFinderType(); data.flagTypes1 = gameSettings->getFlagTypes1(); - for(int i= 0; igetFactionTypeName(i); data.networkPlayerNames[i]= gameSettings->getNetworkPlayerName(i); data.networkPlayerStatuses[i] = gameSettings->getNetworkPlayerStatuses(i); @@ -280,6 +280,16 @@ NetworkMessageLaunch::NetworkMessageLaunch(const GameSettings *gameSettings,int8 data.teams[i]= gameSettings->getTeam(i); data.startLocationIndex[i]= gameSettings->getStartLocationIndex(i); } + for(int i= data.factionCount; i < GameConstants::maxPlayers; ++i) { + data.factionTypeNames[i]= ""; + data.networkPlayerNames[i]= ""; + data.networkPlayerStatuses[i] = 0; + data.networkPlayerLanguages[i] = ""; + data.factionControls[i]= 0; + data.resourceMultiplierIndex[i]= 0; + data.teams[i]= -1; + data.startLocationIndex[i]= 0; + } data.aiAcceptSwitchTeamPercentChance = gameSettings->getAiAcceptSwitchTeamPercentChance(); data.masterserver_admin = gameSettings->getMasterserver_admin(); diff --git a/source/shared_lib/include/platform/sdl/thread.h b/source/shared_lib/include/platform/sdl/thread.h index 1b8a43fa..7d55510e 100644 --- a/source/shared_lib/include/platform/sdl/thread.h +++ b/source/shared_lib/include/platform/sdl/thread.h @@ -23,6 +23,7 @@ #endif //#include "util.h" +#include #include "leak_dumper.h" // ===================================================== @@ -36,7 +37,9 @@ using namespace Shared::PlatformCommon; namespace Shared { namespace Platform { -class Thread{ +class Mutex; + +class Thread { public: enum Priority { pIdle = 0, @@ -49,10 +52,15 @@ public: private: SDL_Thread* thread; + static Mutex mutexthreadList; + static std::vector threadList; + public: Thread(); virtual ~Thread(); + static std::vector getThreadList(); + void start(); virtual void execute()=0; void setPriority(Thread::Priority threadPriority); diff --git a/source/shared_lib/sources/platform/sdl/thread.cpp b/source/shared_lib/sources/platform/sdl/thread.cpp index 5a3fa499..ebd6d2ba 100644 --- a/source/shared_lib/sources/platform/sdl/thread.cpp +++ b/source/shared_lib/sources/platform/sdl/thread.cpp @@ -14,16 +14,23 @@ #include #include "noimpl.h" +#include #include "platform_common.h" using namespace std; -namespace Shared{ namespace Platform{ +namespace Shared { namespace Platform { + +Mutex Thread::mutexthreadList; +std::vector Thread::threadList; // ===================================== // Threads // ===================================== Thread::Thread() { + MutexSafeWrapper safeMutex(&Thread::mutexthreadList); + Thread::threadList.push_back(this); + safeMutex.ReleaseLock(); thread = NULL; } @@ -32,6 +39,19 @@ Thread::~Thread() { SDL_WaitThread(thread, NULL); thread = NULL; } + + MutexSafeWrapper safeMutex(&Thread::mutexthreadList); + std::vector::iterator iterFind = std::find(Thread::threadList.begin(),Thread::threadList.end(),this); + Thread::threadList.erase(iterFind); + safeMutex.ReleaseLock(); +} + +std::vector Thread::getThreadList() { + std::vector result; + MutexSafeWrapper safeMutex(&Thread::mutexthreadList); + result = threadList; + safeMutex.ReleaseLock(); + return result; } void Thread::start() { diff --git a/source/shared_lib/sources/util/util.cpp b/source/shared_lib/sources/util/util.cpp index 369d10e5..8fec8a4a 100644 --- a/source/shared_lib/sources/util/util.cpp +++ b/source/shared_lib/sources/util/util.cpp @@ -177,6 +177,7 @@ std::string SystemFlags::getHTTP(std::string URL,CURL *handle,int timeOut,CURLco CURL *SystemFlags::initHTTP() { if(SystemFlags::curl_global_init_called == false) { SystemFlags::curl_global_init_called = true; + //printf("HTTP init\n"); CURLcode result = curl_global_init(CURL_GLOBAL_ALL); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d] curl_global_init called and returned: result %d [%s]\n",__FILE__,__FUNCTION__,__LINE__,result,curl_easy_strerror(result)); //printf("In [%s::%s Line %d] curl_global_init called and returned: result %d [%s]\n",__FILE__,__FUNCTION__,__LINE__,result,curl_easy_strerror(result)); @@ -190,6 +191,15 @@ CURL *SystemFlags::initHTTP() { return handle; } +void SystemFlags::globalCleanupHTTP() { + if(SystemFlags::curl_global_init_called == true) { + SystemFlags::curl_global_init_called = false; + //printf("HTTP cleanup\n"); + curl_global_cleanup(); + //printf("In [%s::%s Line %d] curl_global_cleanup called\n",__FILE__,__FUNCTION__,__LINE__); + } +} + SystemFlags::SystemFlagsType & SystemFlags::getSystemSettingType(DebugType type) { if(SystemFlags::debugLogFileList == NULL) { if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -271,14 +281,6 @@ SystemFlags::SystemFlags() { } -void SystemFlags::globalCleanupHTTP() { - if(SystemFlags::curl_global_init_called == true) { - SystemFlags::curl_global_init_called = false; - curl_global_cleanup(); - //printf("In [%s::%s Line %d] curl_global_cleanup called\n",__FILE__,__FUNCTION__,__LINE__); - } -} - void SystemFlags::cleanupHTTP(CURL **handle, bool globalCleanup) { if(handle != NULL && *handle != NULL) { curl_easy_cleanup(*handle); @@ -375,8 +377,6 @@ void SystemFlags::Close() { } } - SystemFlags::globalCleanupHTTP(); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); } diff --git a/source/shared_lib/sources/xml/xml_parser.cpp b/source/shared_lib/sources/xml/xml_parser.cpp index e3e2246f..a0de3bc0 100644 --- a/source/shared_lib/sources/xml/xml_parser.cpp +++ b/source/shared_lib/sources/xml/xml_parser.cpp @@ -64,6 +64,7 @@ bool XmlIo::initialized= false; XmlIo::XmlIo() { try{ + //printf("XmlIo init\n"); XMLPlatformUtils::Initialize(); XmlIo::initialized= true; @@ -93,6 +94,7 @@ XmlIo &XmlIo::getInstance() { void XmlIo::cleanup() { if(XmlIo::initialized == true) { XmlIo::initialized= false; + //printf("XmlIo cleanup\n"); XMLPlatformUtils::Terminate(); } }