- more memory cleanup and thread cleanup (found using valgrind)

This commit is contained in:
Mark Vejvoda 2011-09-28 15:32:57 +00:00
parent a3bf4bfe32
commit 43ed4553ea
7 changed files with 63 additions and 14 deletions

View File

@ -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__);

View File

@ -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;
}

View File

@ -270,7 +270,7 @@ NetworkMessageLaunch::NetworkMessageLaunch(const GameSettings *gameSettings,int8
data.pathFinderType = gameSettings->getPathFinderType();
data.flagTypes1 = gameSettings->getFlagTypes1();
for(int i= 0; i<data.factionCount; ++i) {
for(int i= 0; i < data.factionCount; ++i) {
data.factionTypeNames[i]= gameSettings->getFactionTypeName(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();

View File

@ -23,6 +23,7 @@
#endif
//#include "util.h"
#include <vector>
#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<Thread *> threadList;
public:
Thread();
virtual ~Thread();
static std::vector<Thread *> getThreadList();
void start();
virtual void execute()=0;
void setPriority(Thread::Priority threadPriority);

View File

@ -14,16 +14,23 @@
#include <assert.h>
#include "noimpl.h"
#include <algorithm>
#include "platform_common.h"
using namespace std;
namespace Shared{ namespace Platform{
namespace Shared { namespace Platform {
Mutex Thread::mutexthreadList;
std::vector<Thread *> 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<Thread *>::iterator iterFind = std::find(Thread::threadList.begin(),Thread::threadList.end(),this);
Thread::threadList.erase(iterFind);
safeMutex.ReleaseLock();
}
std::vector<Thread *> Thread::getThreadList() {
std::vector<Thread *> result;
MutexSafeWrapper safeMutex(&Thread::mutexthreadList);
result = threadList;
safeMutex.ReleaseLock();
return result;
}
void Thread::start() {

View File

@ -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__);
}

View File

@ -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();
}
}