Parcourir la source

Fix PSRAM allocation safety and memory leaks

SybexXx il y a 5 jours
Parent
commit
8813056443
1 fichiers modifiés avec 19 ajouts et 1 suppressions
  1. 19 1
      code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp

+ 19 - 1
code/components/jomjol_flowcontroll/ClassFlowPostProcessing.cpp

@@ -4,6 +4,7 @@
 
 #include <iomanip>
 #include <sstream>
+#include <new>
 
 #include <time.h>
 
@@ -661,7 +662,20 @@ bool ClassFlowPostProcessing::ReadParameter(FILE* pfile, string& aktparamgraph)
     return true;
 }
 
+ClassFlowPostProcessing::~ClassFlowPostProcessing() {
+    for (NumberPost* number : NUMBERS) {
+        delete number;
+    }
+    NUMBERS.clear();
+}
+
 void ClassFlowPostProcessing::InitNUMBERS() {
+    // The configuration can be reloaded; release any previous per-number state first.
+    for (NumberPost* number : NUMBERS) {
+        delete number;
+    }
+    NUMBERS.clear();
+
     int anzDIGIT = 0;
     int anzANALOG = 0;
     std::vector<std::string> name_numbers;
@@ -679,7 +693,11 @@ void ClassFlowPostProcessing::InitNUMBERS() {
     ESP_LOGD(TAG, "Anzahl NUMBERS: %d - DIGITS: %d, ANALOG: %d", name_numbers.size(), anzDIGIT, anzANALOG);
 
     for (int _num = 0; _num < name_numbers.size(); ++_num) {
-        NumberPost *_number = new NumberPost;
+        NumberPost *_number = new (std::nothrow) NumberPost;
+        if (_number == NULL) {
+            LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Unable to allocate post-processing state for number " + name_numbers[_num]);
+            return;
+        }
 
         _number->name = name_numbers[_num];