From 467b2468582062fb87737f0feafc00f15efc0c9f Mon Sep 17 00:00:00 2001 From: andy5995 Date: Sun, 30 Mar 2025 13:51:41 -0500 Subject: [PATCH] Fix warnings: throw will always call terminate --- source/glest_game/graphics/renderer.cpp | 56 +++++-------- .../sources/platform/common/base_thread.cpp | 81 ++++++------------- 2 files changed, 45 insertions(+), 92 deletions(-) diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index c32e37990..66df040f3 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -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; } } diff --git a/source/shared_lib/sources/platform/common/base_thread.cpp b/source/shared_lib/sources/platform/common/base_thread.cpp index e2758e1e1..9a3cf00fa 100644 --- a/source/shared_lib/sources/platform/common/base_thread.cpp +++ b/source/shared_lib/sources/platform/common/base_thread.cpp @@ -57,12 +57,10 @@ 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(this); if (this->getStarted() == false) { time_t elapsed = time(NULL); for (; this->getStarted() == false && difftime((long int)time(NULL), elapsed) <= 3;) { @@ -70,84 +68,51 @@ BaseThread::~BaseThread() { } } - 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(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() {