Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 22 additions & 34 deletions source/glest_game/graphics/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,62 +308,50 @@ Renderer::~Renderer() {
__LINE__);

delete modelRenderer;
modelRenderer = NULL;
modelRenderer = nullptr;
delete textRenderer;
textRenderer = NULL;
textRenderer = nullptr;
delete textRenderer3D;
textRenderer3D = NULL;
textRenderer3D = nullptr;
delete particleRenderer;
particleRenderer = NULL;
particleRenderer = nullptr;

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; i < rsCount; ++i) {
delete modelManager[i];
modelManager[i] = NULL;
modelManager[i] = nullptr;
delete textureManager[i];
textureManager[i] = NULL;
textureManager[i] = nullptr;
delete particleManager[i];
particleManager[i] = NULL;
particleManager[i] = nullptr;
delete fontManager[i];
fontManager[i] = NULL;
fontManager[i] = nullptr;
}

if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled)
SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__,
__LINE__);

// Wait for the queue to become empty or timeout the thread at 7 seconds
cleanupScreenshotThread();

if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled)
SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__,
__LINE__);

mapSurfaceData.clear();
quadCache = VisibleQuadContainerCache();
quadCache.clearFrustumData();

if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled)
SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__,
__LINE__);

this->menu = NULL;
this->game = NULL;
this->gameCamera = NULL;
this->menu = nullptr;
this->game = nullptr;
this->gameCamera = nullptr;

delete saveScreenShotThreadAccessor;
saveScreenShotThreadAccessor = NULL;
} catch (const exception &e) {
saveScreenShotThreadAccessor = nullptr;
} catch (const std::exception &e) {
// Log the error instead of throwing an exception
char szBuf[8096] = "";
snprintf(szBuf, 8096, "In [%s::%s Line: %d]\nError [%s]\n", extractFileFromDirectoryPath(__FILE__).c_str(), __FUNCTION__, __LINE__, e.what());
snprintf(szBuf, sizeof(szBuf), "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);

throw megaglest_runtime_error(szBuf);
if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) {
SystemFlags::OutputDebug(SystemFlags::debugSystem, szBuf);
}

// Optionally store the error message for later debugging
// lastErrorMessage = szBuf;
}
}

Expand Down
81 changes: 23 additions & 58 deletions source/shared_lib/sources/platform/common/base_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,97 +57,62 @@ Mutex *BaseThread::getMutexThreadObjectAccessor() {
}

BaseThread::~BaseThread() {
// printf("In ~BaseThread Line: %d uniqueID
// [%s]\n",__LINE__,uniqueID.c_str());
if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled)
if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) {
SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] uniqueID [%s]\n", __FILE__, __FUNCTION__, __LINE__, uniqueID.c_str());
}

// BaseThread *base_thread = dynamic_cast<BaseThread *>(this);
if (this->getStarted() == false) {
time_t elapsed = time(NULL);
for (; this->getStarted() == false && difftime((long int)time(NULL), elapsed) <= 3;) {
sleep(5);
}
}

if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled)
SystemFlags::OutputDebug(SystemFlags::debugSystem, "In [%s::%s Line: %d] uniqueID [%s]\n", __FILE__, __FUNCTION__, __LINE__, uniqueID.c_str());
bool ret = shutdownAndWait();
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);

// printf("In ~BaseThread Line: %d uniqueID
// [%s]\n",__LINE__,uniqueID.c_str());
if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) {
SystemFlags::OutputDebug(SystemFlags::debugSystem, "shutdownAndWait() returned [%d] for uniqueID [%s]\n", ret, uniqueID.c_str());
}

MutexSafeWrapper safeMutexMasterList(&mutexMasterThreadList);

// printf("In ~BaseThread Line: %d uniqueID
// [%s]\n",__LINE__,uniqueID.c_str());

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

// printf("In ~BaseThread Line: %d uniqueID
// [%s]\n",__LINE__,uniqueID.c_str());

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);
// **Instead of throwing an exception, log an error message**
if (SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) {
SystemFlags::OutputDebug(SystemFlags::debugSystem, "ERROR: Invalid thread delete for ptr: %p\n", this);
}

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);
// Optionally, store the error for later handling instead of throwing
//lastErrorMessage = "Invalid thread delete for ptr: " + std::to_string(reinterpret_cast<uintptr_t>(this));

// printf("In ~BaseThread Line: %d uniqueID
// [%s]\n",__LINE__,uniqueID.c_str());
// Return early to prevent further issues
return;
}

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

// printf("In ~BaseThread Line: %d uniqueID
// [%s]\n",__LINE__,uniqueID.c_str());

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

// Safe cleanup of dynamically allocated mutexes
delete mutexRunning;
mutexRunning = NULL;
delete mutexQuit;
mutexQuit = NULL;
delete mutexBeginExecution;
mutexBeginExecution = NULL;
delete mutexDeleteSelfOnExecutionDone;
mutexDeleteSelfOnExecutionDone = NULL;
delete mutexThreadObjectAccessor;
mutexThreadObjectAccessor = NULL;
delete mutexThreadOwnerValid;
mutexThreadOwnerValid = NULL;
delete mutexExecutingTask;
mutexExecutingTask = NULL;
delete mutexStarted;
mutexStarted = NULL;

// printf("In ~BaseThread Line: %d uniqueID [%s]
// [%p]\n",__LINE__,uniqueID.c_str(),this);
mutexRunning = nullptr;
mutexQuit = nullptr;
mutexBeginExecution = nullptr;
mutexDeleteSelfOnExecutionDone = nullptr;
mutexThreadObjectAccessor = nullptr;
mutexThreadOwnerValid = nullptr;
mutexExecutingTask = nullptr;
mutexStarted = nullptr;
}

bool BaseThread::getStarted() {
Expand Down
Loading