- bugfixes related to cppcheck report

- bugfixes found using valgrind (memory leaks)
This commit is contained in:
Mark Vejvoda
2011-12-02 16:07:59 +00:00
parent 4885f3f3a6
commit d33e1174a5
59 changed files with 772 additions and 272 deletions

View File

@@ -94,29 +94,38 @@ void Thread::resume() {
class SDLMutexSafeWrapper {
protected:
SDL_mutex *mutex;
SDL_mutex **mutex;
bool destroyMutexInDestructor;
public:
SDLMutexSafeWrapper(SDL_mutex *mutex) {
SDLMutexSafeWrapper(SDL_mutex **mutex, bool destroyMutexInDestructor=false) {
this->mutex = mutex;
this->destroyMutexInDestructor = destroyMutexInDestructor;
Lock();
}
~SDLMutexSafeWrapper() {
ReleaseLock();
bool keepMutex = (this->destroyMutexInDestructor == true && mutex != NULL && *mutex != NULL);
ReleaseLock(keepMutex);
if(this->destroyMutexInDestructor == true && mutex != NULL && *mutex != NULL) {
SDL_DestroyMutex(*mutex);
*mutex = NULL;
mutex = NULL;
}
}
void Lock() {
if(this->mutex != NULL) {
SDL_mutexP(this->mutex);
if(mutex != NULL && *mutex != NULL) {
SDL_mutexP(*mutex);
}
}
void ReleaseLock(bool keepMutex=false) {
if(this->mutex != NULL) {
SDL_mutexV(this->mutex);
if(mutex != NULL && *mutex != NULL) {
SDL_mutexV(*mutex);
if(keepMutex == false) {
this->mutex = NULL;
mutex = NULL;
}
}
}
@@ -124,7 +133,7 @@ public:
Mutex::Mutex(string ownerId) {
mutexAccessor = SDL_CreateMutex();
SDLMutexSafeWrapper safeMutex(mutexAccessor);
SDLMutexSafeWrapper safeMutex(&mutexAccessor);
refCount=0;
this->ownerId = ownerId;
mutex = SDL_CreateMutex();
@@ -138,7 +147,7 @@ Mutex::Mutex(string ownerId) {
}
Mutex::~Mutex() {
SDLMutexSafeWrapper safeMutex(mutexAccessor);
SDLMutexSafeWrapper safeMutex(&mutexAccessor,true);
if(mutex == NULL) {
char szBuf[1024]="";
snprintf(szBuf,1023,"In [%s::%s Line: %d] mutex == NULL refCount = %d owner [%s] deleteownerId [%s]",__FILE__,__FUNCTION__,__LINE__,refCount,ownerId.c_str(),deleteownerId.c_str());