changeset 6430:81f8bfc1a700

properly fixing throwInNoexceptFunction reported by cppcheck
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 18 Nov 2025 16:34:18 +0100
parents 2d5910d39c47
children bbb80ae2d652
files OrthancFramework/Sources/Cache/MemoryCache.cpp OrthancFramework/Sources/Cache/MemoryObjectCache.cpp OrthancFramework/Sources/Cache/MemoryStringCache.cpp OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp OrthancFramework/Sources/MallocMemoryBuffer.cpp OrthancFramework/Sources/MallocMemoryBuffer.h OrthancFramework/Sources/MetricsRegistry.cpp OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h OrthancServer/Resources/RunCppCheck-2.1.sh OrthancServer/Resources/RunCppCheck-2.17.0.sh OrthancServer/Resources/RunCppCheck-2.17.1.sh OrthancServer/Sources/LuaScripting.cpp OrthancServer/Sources/StorageCommitmentReports.cpp
diffstat 14 files changed, 202 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/Cache/MemoryCache.cpp	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancFramework/Sources/Cache/MemoryCache.cpp	Tue Nov 18 16:34:18 2025 +0100
@@ -88,9 +88,19 @@
       while (!index_.IsEmpty())
       {
         Page* element = NULL;
-        index_.RemoveOldest(element);
-        assert(element != NULL);
-        delete element;
+
+        try
+        {
+          index_.RemoveOldest(element);
+
+          assert(element != NULL);
+          delete element;
+        }
+        catch (OrthancException& e)
+        {
+          // Don't throw exceptions in destructors
+          LOG(ERROR) << "Exception in destructor: " << e.What();
+        }
       }
     }
 
--- a/OrthancFramework/Sources/Cache/MemoryObjectCache.cpp	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancFramework/Sources/Cache/MemoryObjectCache.cpp	Tue Nov 18 16:34:18 2025 +0100
@@ -26,6 +26,7 @@
 #include "MemoryObjectCache.h"
 
 #include "../Compatibility.h"
+#include "../Logging.h"
 
 namespace Orthanc
 {
@@ -96,7 +97,16 @@
 
   MemoryObjectCache::~MemoryObjectCache()
   {
-    Recycle(0);
+    try
+    {
+      Recycle(0);
+    }
+    catch (OrthancException& e)
+    {
+      // Don't throw exceptions in destructors
+      LOG(ERROR) << "Exception in destructor: " << e.What();
+    }
+
     assert(content_.IsEmpty());
   }
 
--- a/OrthancFramework/Sources/Cache/MemoryStringCache.cpp	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancFramework/Sources/Cache/MemoryStringCache.cpp	Tue Nov 18 16:34:18 2025 +0100
@@ -25,6 +25,8 @@
 #include "../PrecompiledHeaders.h"
 #include "MemoryStringCache.h"
 
+#include "../Logging.h"
+
 namespace Orthanc
 {
   class MemoryStringCache::StringValue : public ICacheable
@@ -118,7 +120,16 @@
 
   MemoryStringCache::~MemoryStringCache()
   {
-    Recycle(0);
+    try
+    {
+      Recycle(0);
+    }
+    catch (OrthancException& e)
+    {
+      // Don't throw exceptions in destructors
+      LOG(ERROR) << "Exception in destructor: " << e.What();
+    }
+
     assert(content_.IsEmpty());
   }
 
--- a/OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp	Tue Nov 18 16:34:18 2025 +0100
@@ -1300,27 +1300,35 @@
     {
       boost::mutex::scoped_lock lock(registry_.mutex_);
 
-      switch (targetState_)
+      try
       {
-        case JobState_Failure:
-          registry_.MarkRunningAsCompleted
-            (*handler_, canceled_ ? CompletedReason_Canceled : CompletedReason_Failure);
-          break;
+        switch (targetState_)
+        {
+          case JobState_Failure:
+            registry_.MarkRunningAsCompleted
+              (*handler_, canceled_ ? CompletedReason_Canceled : CompletedReason_Failure);
+            break;
 
-        case JobState_Success:
-          registry_.MarkRunningAsCompleted(*handler_, CompletedReason_Success);
-          break;
+          case JobState_Success:
+            registry_.MarkRunningAsCompleted(*handler_, CompletedReason_Success);
+            break;
 
-        case JobState_Paused:
-          registry_.MarkRunningAsPaused(*handler_);
-          break;
+          case JobState_Paused:
+            registry_.MarkRunningAsPaused(*handler_);
+            break;
+
+          case JobState_Retry:
+            registry_.MarkRunningAsRetry(*handler_, targetRetryTimeout_);
+            break;
 
-        case JobState_Retry:
-          registry_.MarkRunningAsRetry(*handler_, targetRetryTimeout_);
-          break;
-
-        default:
-          assert(0);
+          default:
+            assert(0);
+        }
+      }
+      catch (OrthancException& e)
+      {
+        // Don't throw exceptions in destructors
+        LOG(ERROR) << "Exception in destructor: " << e.What();
       }
     }
   }
--- a/OrthancFramework/Sources/MallocMemoryBuffer.cpp	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancFramework/Sources/MallocMemoryBuffer.cpp	Tue Nov 18 16:34:18 2025 +0100
@@ -25,6 +25,7 @@
 #include "PrecompiledHeaders.h"
 #include "MallocMemoryBuffer.h"
 
+#include "Logging.h"
 #include "OrthancException.h"
 
 #include <string.h>
@@ -40,6 +41,20 @@
   }
 
 
+  MallocMemoryBuffer::~MallocMemoryBuffer()
+  {
+    try
+    {
+      Clear();
+    }
+    catch (OrthancException& e)
+    {
+      // Don't throw exceptions in destructors
+      LOG(ERROR) << "Exception in destructor: " << e.What();
+    }
+  }
+
+
   void MallocMemoryBuffer::Clear()
   {
     if (size_ != 0)
--- a/OrthancFramework/Sources/MallocMemoryBuffer.h	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancFramework/Sources/MallocMemoryBuffer.h	Tue Nov 18 16:34:18 2025 +0100
@@ -45,10 +45,7 @@
   public:
     MallocMemoryBuffer();
 
-    virtual ~MallocMemoryBuffer() ORTHANC_OVERRIDE
-    {
-      Clear();
-    }
+    virtual ~MallocMemoryBuffer() ORTHANC_OVERRIDE;
 
     void Clear();
 
--- a/OrthancFramework/Sources/MetricsRegistry.cpp	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancFramework/Sources/MetricsRegistry.cpp	Tue Nov 18 16:34:18 2025 +0100
@@ -27,6 +27,7 @@
 
 #include "ChunkedBuffer.h"
 #include "Compatibility.h"
+#include "Logging.h"
 #include "OrthancException.h"
 
 #include <boost/math/special_functions/round.hpp>
@@ -617,7 +618,15 @@
 
   MetricsRegistry::ActiveCounter::~ActiveCounter()
   {
-    metrics_.Add(-1);
+    try
+    {
+      metrics_.Add(-1);
+    }
+    catch (OrthancException& e)
+    {
+      // Don't throw exceptions in destructors
+      LOG(ERROR) << "Exception in destructor: " << e.What();
+    }
   }
 
 
@@ -629,7 +638,15 @@
 
   MetricsRegistry::AvailableResourcesDecounter::~AvailableResourcesDecounter()
   {
-    metrics_.Add(1);
+    try
+    {
+      metrics_.Add(1);
+    }
+    catch (OrthancException& e)
+    {
+      // Don't throw exceptions in destructors
+      LOG(ERROR) << "Exception in destructor: " << e.What();
+    }
   }
 
 
@@ -673,7 +690,16 @@
     if (active_)
     {
       boost::posix_time::time_duration diff = GetNow() - start_;
-      registry_.SetIntegerValue(name_, static_cast<int64_t>(diff.total_milliseconds()), policy_);
+
+      try
+      {
+        registry_.SetIntegerValue(name_, static_cast<int64_t>(diff.total_milliseconds()), policy_);
+      }
+      catch (OrthancException& e)
+      {
+        // Don't throw exceptions in destructors
+        LOG(ERROR) << "Exception in destructor: " << e.What();
+      }
     }
   }
 }
--- a/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp	Tue Nov 18 16:34:18 2025 +0100
@@ -221,6 +221,19 @@
   }
 
 
+  MemoryBuffer::~MemoryBuffer()
+  {
+    try
+    {
+      Clear();
+    }
+    catch (ORTHANC_PLUGINS_EXCEPTION_CLASS&)
+    {
+      // Don't throw exceptions in destructors
+    }
+  }
+
+
   void MemoryBuffer::Clear()
   {
     if (buffer_.data != NULL)
@@ -652,6 +665,19 @@
   }
 
 
+  OrthancString::~OrthancString()
+  {
+    try
+    {
+      Clear();
+    }
+    catch (ORTHANC_PLUGINS_EXCEPTION_CLASS&)
+    {
+      // Don't throw exceptions in destructors
+    }
+  }
+
+
   void OrthancString::Assign(char* str)
   {
     Clear();
@@ -1301,6 +1327,20 @@
     }
   }
 
+
+  OrthancImage::~OrthancImage()
+  {
+    try
+    {
+      Clear();
+    }
+    catch (ORTHANC_PLUGINS_EXCEPTION_CLASS&)
+    {
+      // Don't throw exceptions in destructors
+    }
+  }
+
+
   void OrthancImage::UncompressPngImage(const void* data,
                                         size_t size)
   {
--- a/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h	Tue Nov 18 16:34:18 2025 +0100
@@ -213,10 +213,7 @@
   public:
     MemoryBuffer();
 
-    ~MemoryBuffer()
-    {
-      Clear();
-    }
+    ~MemoryBuffer();
 
     OrthancPluginMemoryBuffer* operator*()
     {
@@ -371,10 +368,7 @@
     {
     }
 
-    ~OrthancString()
-    {
-      Clear();
-    }
+    ~OrthancString();
 
     // This transfers ownership, warning: The string must have been
     // allocated by the Orthanc core
@@ -491,10 +485,7 @@
                  uint32_t                  pitch,
                  void*                     buffer);
 
-    ~OrthancImage()
-    {
-      Clear();
-    }
+    ~OrthancImage();
 
     void UncompressPngImage(const void* data,
                             size_t size);
--- a/OrthancServer/Resources/RunCppCheck-2.1.sh	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancServer/Resources/RunCppCheck-2.1.sh	Tue Nov 18 16:34:18 2025 +0100
@@ -41,7 +41,7 @@
 assertWithSideEffect:../../OrthancFramework/Sources/DicomNetworking/Internals/CommandDispatcher.cpp:454
 EOF
 
-${CPPCHECK} --enable=all --quiet --std=c++11 \
+${CPPCHECK} -j8 --enable=all --quiet --std=c++11 \
             --suppressions-list=/tmp/cppcheck-suppressions.txt \
             -DBOOST_HAS_DATE_TIME=1 \
             -DBOOST_HAS_FILESYSTEM_V3=1 \
--- a/OrthancServer/Resources/RunCppCheck-2.17.0.sh	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancServer/Resources/RunCppCheck-2.17.0.sh	Tue Nov 18 16:34:18 2025 +0100
@@ -37,7 +37,7 @@
 assertWithSideEffect:../../OrthancFramework/Sources/DicomNetworking/Internals/CommandDispatcher.cpp:454
 EOF
 
-${CPPCHECK} --enable=all --std=gnu++11 --library=boost \
+${CPPCHECK} -j8 --enable=all --std=gnu++11 --library=boost \
             --suppressions-list=/tmp/cppcheck-suppressions.txt \
             -I/usr/include/ \
             -I/usr/include/jsoncpp/ \
--- a/OrthancServer/Resources/RunCppCheck-2.17.1.sh	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancServer/Resources/RunCppCheck-2.17.1.sh	Tue Nov 18 16:34:18 2025 +0100
@@ -19,7 +19,6 @@
 syntaxError:../../OrthancFramework/UnitTestsSources/DicomMapTests.cpp:74
 syntaxError:../../OrthancFramework/UnitTestsSources/ZipTests.cpp:133
 syntaxError:../../OrthancServer/UnitTestsSources/UnitTestsMain.cpp:325
-uninitMemberVar:../../OrthancServer/Sources/ServerJobs/StorageCommitmentScpJob.cpp:419
 useInitializationList:../../OrthancFramework/Sources/Images/PngReader.cpp:91
 useInitializationList:../../OrthancFramework/Sources/Images/PngWriter.cpp:99
 useInitializationList:../../OrthancServer/Sources/ServerJobs/DicomModalityStoreJob.cpp:275
@@ -41,19 +40,6 @@
 knownConditionTrueFalse:../../OrthancFramework/Sources/DicomNetworking/Internals/CommandDispatcher.cpp:114
 knownConditionTrueFalse:../../OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.cpp:425
 knownConditionTrueFalse:../../OrthancFramework/Sources/JobsEngine/Operations/SequenceOfOperationsJob.cpp:345
-throwInNoexceptFunction:../../OrthancFramework/Sources/Cache/MemoryStringCache.cpp:121
-throwInNoexceptFunction:../../OrthancFramework/Sources/Cache/MemoryCache.cpp:91
-throwInNoexceptFunction:../../OrthancFramework/Sources/Cache/MemoryObjectCache.cpp:99
-throwInNoexceptFunction:../../OrthancFramework/Sources/MallocMemoryBuffer.h:50
-throwInNoexceptFunction:../../OrthancFramework/Sources/MetricsRegistry.cpp:620
-throwInNoexceptFunction:../../OrthancFramework/Sources/MetricsRegistry.cpp:632
-throwInNoexceptFunction:../../OrthancFramework/Sources/MetricsRegistry.cpp:676
-throwInNoexceptFunction:../../OrthancFramework/Sources/JobsEngine/JobsRegistry.cpp:1296
-throwInNoexceptFunction:../../OrthancServer/Sources/LuaScripting.cpp:830
-throwInNoexceptFunction:../../OrthancServer/Sources/StorageCommitmentReports.cpp:187
-throwInNoexceptFunction:../../OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h:218
-throwInNoexceptFunction:../../OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h:376
-throwInNoexceptFunction:../../OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.h:496
 rethrowNoCurrentException:../../OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp
 rethrowNoCurrentException:../../OrthancFramework/UnitTestsSources/RestApiTests.cpp
 constParameterPointer:../../OrthancFramework/Sources/Toolbox.cpp:3046
@@ -65,8 +51,12 @@
 unknownMacro:../../OrthancFramework/Sources/DicomParsing/DicomModification.cpp:39
 EOF
 
+CPPCHECK_BUILD_DIR=/tmp/cppcheck-build-dir/
+mkdir -p ${CPPCHECK_BUILD_DIR}
 
-${CPPCHECK} -j 8 --enable=all --quiet --std=c++11 \
+${CPPCHECK} -j8 --enable=all --quiet --std=c++11 \
+            --cppcheck-build-dir=${CPPCHECK_BUILD_DIR} \
+            --suppress=unusedFunction \
             --suppress=missingIncludeSystem \
             --suppress=missingInclude \
             --suppress=useStlAlgorithm \
@@ -126,11 +116,29 @@
             ../../OrthancServer/Sources \
             ../../OrthancServer/UnitTestsSources \
             ../../OrthancServer/Plugins/Samples/Common \
-            ../../OrthancServer/Plugins/Samples/ConnectivityChecks \
-            ../../OrthancServer/Plugins/Samples/DelayedDeletion \
-            ../../OrthancServer/Plugins/Samples/Housekeeper \
-            ../../OrthancServer/Plugins/Samples/ModalityWorklists \
-            ../../OrthancServer/Plugins/Samples/MultitenantDicom \
-            ../../OrthancServer/Plugins/Samples/AdoptDicomInstance \
+            \
+            ../../OrthancServer/Plugins/Samples/AdoptDicomInstance/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/AutomatedJpeg2kCompression/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/Common/OrthancPluginCppWrapper.cpp \
+            ../../OrthancServer/Plugins/Samples/ConnectivityChecks/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/CppSkeleton/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/CustomImageDecoder/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/DelayedDeletion/LargeDeleteJob.cpp \
+            ../../OrthancServer/Plugins/Samples/DelayedDeletion/PendingDeletionsDatabase.cpp \
+            ../../OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/Housekeeper/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/ModalityWorklists/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/MultitenantDicom/DicomFilter.cpp \
+            ../../OrthancServer/Plugins/Samples/MultitenantDicom/FindRequestHandler.cpp \
+            ../../OrthancServer/Plugins/Samples/MultitenantDicom/MoveRequestHandler.cpp \
+            ../../OrthancServer/Plugins/Samples/MultitenantDicom/MultitenantDicomServer.cpp \
+            ../../OrthancServer/Plugins/Samples/MultitenantDicom/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/MultitenantDicom/PluginToolbox.cpp \
+            ../../OrthancServer/Plugins/Samples/MultitenantDicom/StoreRequestHandler.cpp \
+            ../../OrthancServer/Plugins/Samples/Sanitizer/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/StorageArea/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/StorageCommitmentScp/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/WebDavFilesystem/Plugin.cpp \
+            ../../OrthancServer/Plugins/Samples/WebSkeleton/Framework/Plugin.cpp \
             \
             2>&1
--- a/OrthancServer/Sources/LuaScripting.cpp	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancServer/Sources/LuaScripting.cpp	Tue Nov 18 16:34:18 2025 +0100
@@ -827,7 +827,16 @@
     if (state_ == State_Running)
     {
       LOG(ERROR) << "INTERNAL ERROR: LuaScripting::Stop() should be invoked manually to avoid mess in the destruction order!";
-      Stop();
+
+      try
+      {
+        Stop();
+      }
+      catch (OrthancException& e)
+      {
+        // Don't throw exceptions in destructors
+        LOG(ERROR) << "Exception in destructor: " << e.What();
+      }
     }
   }
 
--- a/OrthancServer/Sources/StorageCommitmentReports.cpp	Tue Nov 18 15:28:09 2025 +0100
+++ b/OrthancServer/Sources/StorageCommitmentReports.cpp	Tue Nov 18 16:34:18 2025 +0100
@@ -24,6 +24,7 @@
 #include "PrecompiledHeadersServer.h"
 #include "StorageCommitmentReports.h"
 
+#include "../../OrthancFramework/Sources/Logging.h"
 #include "../../OrthancFramework/Sources/OrthancException.h"
 
 namespace Orthanc
@@ -184,10 +185,18 @@
     while (!content_.IsEmpty())
     {
       Report* report = NULL;
-      content_.RemoveOldest(report);
 
-      assert(report != NULL);
-      delete report;
+      try
+      {
+        content_.RemoveOldest(report);
+        assert(report != NULL);
+        delete report;
+      }
+      catch (OrthancException& e)
+      {
+        // Don't throw exceptions in destructors
+        LOG(ERROR) << "Exception in destructor: " << e.What();
+      }
     }
   }