From da97a25cf02b8b748c4089972053d65ad0acc796 Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Mon, 25 Mar 2024 17:43:15 +0100
Subject: [PATCH] Correct valgrind definitely lost leaks

---
 src/cluster/cluster.cpp       |   1 +
 src/include/List.h            |   1 +
 src/libnptm/Commons.cpp       |   3 -
 src/libnptm/Configuration.cpp | 111 ++++++++++++++++++----------------
 src/libnptm/Parsers.cpp       |  11 ++--
 5 files changed, 67 insertions(+), 60 deletions(-)

diff --git a/src/cluster/cluster.cpp b/src/cluster/cluster.cpp
index 620bd5c4..af292c1b 100644
--- a/src/cluster/cluster.cpp
+++ b/src/cluster/cluster.cpp
@@ -747,6 +747,7 @@ void cluster(string config_file, string data_file, string output_path) {
     delete[] zpv;
     delete c1;
     delete c1ao;
+    delete c2;
     delete c3;
     delete c4;
     delete c6;
diff --git a/src/include/List.h b/src/include/List.h
index f97ebc60..6d2d7427 100644
--- a/src/include/List.h
+++ b/src/include/List.h
@@ -73,6 +73,7 @@ public:
       current = old->p_prev;
       delete old;
     }
+    delete current;
   }
   
   /*! \brief Append an element at the end of the List.
diff --git a/src/libnptm/Commons.cpp b/src/libnptm/Commons.cpp
index 6d6ec51f..b1049638 100644
--- a/src/libnptm/Commons.cpp
+++ b/src/libnptm/Commons.cpp
@@ -232,9 +232,6 @@ C1_AddOns::C1_AddOns(C4 *c4) {
   ecscp = new dcomplex[2]();
   scscpm = new dcomplex[2]();
   ecscpm = new dcomplex[2]();
-  v3j0 = new double[nv3j]();
-  ind3j = new int*[lmpo];
-  for (int ii = 0; ii < lmpo; ii++) ind3j[ii] = new int[lm]();
   sscs = new double[nsph]();
   ecscm = new double[2]();
   scscm = new double[2]();
diff --git a/src/libnptm/Configuration.cpp b/src/libnptm/Configuration.cpp
index fa91d648..80fb5ecd 100644
--- a/src/libnptm/Configuration.cpp
+++ b/src/libnptm/Configuration.cpp
@@ -228,6 +228,7 @@ GeometryConfiguration* GeometryConfiguration::from_legacy(string file_name) {
 							  sc_ph_start, sc_ph_step, sc_ph_end,
 							  _jwtm
   );
+  delete[] file_lines;
   return conf;
 }
 
@@ -322,6 +323,7 @@ ScattererConfiguration::~ScattererConfiguration() {
     for (int j = 0; j < number_of_spheres; j++) {
       delete[] dc0_matrix[i][j];
     }
+    delete[] dc0_matrix[i];
   }
   delete[] dc0_matrix;
   for (int i = 0; i < configurations; i++) {
@@ -400,7 +402,7 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
     variable_name = "XIV";
     if (instpc < 1) { // The variable vector is explicitly defined.
       double xi;
-      List<double> xi_vector;
+      List<double> *xi_vector = new List<double>(1);
       str_target = file_lines[++last_read_line];
       re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?[0-9]+)?");
       regex_search(str_target, m, re);
@@ -408,7 +410,7 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
       str_number = regex_replace(str_number, regex("D"), "e");
       str_number = regex_replace(str_number, regex("d"), "e");
       xi = stod(str_number);
-      xi_vector.set(0, xi);
+      xi_vector->set(0, xi);
       for (int jxi310 = 1; jxi310 < nxi; jxi310++) {
 	str_target = file_lines[++last_read_line];
 	regex_search(str_target, m, re);
@@ -416,9 +418,10 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
 	str_number = regex_replace(str_number, regex("D"), "e");
 	str_number = regex_replace(str_number, regex("d"), "e");
 	xi = stod(str_number);
-	xi_vector.append(xi);
+	xi_vector->append(xi);
       }
-      variable_vector = xi_vector.to_array();
+      variable_vector = xi_vector->to_array();
+      delete xi_vector;
     } else { // instpc >= 1: the variable vector is defined in steps
       double xi, xi_step;
       str_target = file_lines[++last_read_line];
@@ -871,60 +874,60 @@ void ScattererConfiguration::write_binary(string file_name, string mode) {
 
 void ScattererConfiguration::write_hdf5(string file_name) {
   int ies = (use_external_sphere)? 1 : 0;
-  List<string> rec_name_list(1);
-  List<string> rec_type_list(1);
-  List<void *> rec_ptr_list(1);
+  List<string> *rec_name_list = new List<string>(1);
+  List<string> *rec_type_list = new List<string>(1);
+  List<void *> *rec_ptr_list = new List<void *>(1);
   string str_type, str_name;
   int configurations = 0;
   for (int ci = 1; ci <= number_of_spheres; ci++) {
     if(iog_vec[ci - 1] >= ci) configurations++;
   }
-  rec_name_list.set(0, "NSPH");
-  rec_type_list.set(0, "INT32_(1)");
-  rec_ptr_list.set(0, &number_of_spheres);
-  rec_name_list.append("IES");
-  rec_type_list.append("INT32_(1)");
-  rec_ptr_list.append(&ies);
-  rec_name_list.append("IOGVEC");
+  rec_name_list->set(0, "NSPH");
+  rec_type_list->set(0, "INT32_(1)");
+  rec_ptr_list->set(0, &number_of_spheres);
+  rec_name_list->append("IES");
+  rec_type_list->append("INT32_(1)");
+  rec_ptr_list->append(&ies);
+  rec_name_list->append("IOGVEC");
   str_type = "INT32_(" + to_string(number_of_spheres) + ")";
-  rec_type_list.append(str_type);
-  rec_ptr_list.append(iog_vec);
-  rec_name_list.append("EXDC");
-  rec_type_list.append("FLOAT64_(1)");
-  rec_ptr_list.append(&exdc);
-  rec_name_list.append("WP");
-  rec_type_list.append("FLOAT64_(1)");
-  rec_ptr_list.append(&wp);
-  rec_name_list.append("XIP");
-  rec_type_list.append("FLOAT64_(1)");
-  rec_ptr_list.append(&xip);
-  rec_name_list.append("IDFC");
-  rec_type_list.append("INT32_(1)");
-  rec_ptr_list.append(&idfc);
-  rec_name_list.append("NXI");
-  rec_type_list.append("INT32_(1)");
-  rec_ptr_list.append(&number_of_scales);
-  rec_name_list.append("XIVEC");
+  rec_type_list->append(str_type);
+  rec_ptr_list->append(iog_vec);
+  rec_name_list->append("EXDC");
+  rec_type_list->append("FLOAT64_(1)");
+  rec_ptr_list->append(&exdc);
+  rec_name_list->append("WP");
+  rec_type_list->append("FLOAT64_(1)");
+  rec_ptr_list->append(&wp);
+  rec_name_list->append("XIP");
+  rec_type_list->append("FLOAT64_(1)");
+  rec_ptr_list->append(&xip);
+  rec_name_list->append("IDFC");
+  rec_type_list->append("INT32_(1)");
+  rec_ptr_list->append(&idfc);
+  rec_name_list->append("NXI");
+  rec_type_list->append("INT32_(1)");
+  rec_ptr_list->append(&number_of_scales);
+  rec_name_list->append("XIVEC");
   str_type = "FLOAT64_(" + to_string(number_of_scales) + ")";
-  rec_type_list.append(str_type);
-  rec_ptr_list.append(scale_vec);
+  rec_type_list->append(str_type);
+  rec_ptr_list->append(scale_vec);
   for (int i115 = 1; i115 <= number_of_spheres; i115++) {
     if (iog_vec[i115 - 1] < i115) continue;
     str_name = "NSHL_" + to_string(i115);
-    rec_name_list.append(str_name);
-    rec_type_list.append("INT32_(1)");
-    rec_ptr_list.append(&(nshl_vec[i115 - 1])); // was not from IOG
+    rec_name_list->append(str_name);
+    rec_type_list->append("INT32_(1)");
+    rec_ptr_list->append(&(nshl_vec[i115 - 1])); // was not from IOG
     str_name = "ROS_" + to_string(i115);
-    rec_name_list.append(str_name);
-    rec_type_list.append("FLOAT64_(1)");
-    rec_ptr_list.append(&(radii_of_spheres[i115 - 1])); // was not from IOG
+    rec_name_list->append(str_name);
+    rec_type_list->append("FLOAT64_(1)");
+    rec_ptr_list->append(&(radii_of_spheres[i115 - 1])); // was not from IOG
     int nsh = nshl_vec[i115 - 1]; // was not from IOG
     if (i115 == 1) nsh += ies;
     str_name = "RCF_" + to_string(i115); // was not from IOG
     str_type = "FLOAT64_(" + to_string(nsh) + ")";
-    rec_name_list.append(str_name);
-    rec_type_list.append(str_type);
-    rec_ptr_list.append(&(rcf[i115 - 1][0])); // was not from IOG
+    rec_name_list->append(str_name);
+    rec_type_list->append(str_type);
+    rec_ptr_list->append(&(rcf[i115 - 1][0])); // was not from IOG
   }
 
   int dim3 = (idfc == 0) ? number_of_scales : 1;
@@ -949,25 +952,29 @@ void ScattererConfiguration::write_hdf5(string file_name) {
     }
   }
   str_type = "FLOAT64_(" + to_string(dc0m_size) + ")";
-  rec_name_list.append("DC0M");
-  rec_type_list.append(str_type);
-  rec_ptr_list.append(dc0m);
+  rec_name_list->append("DC0M");
+  rec_type_list->append(str_type);
+  rec_ptr_list->append(dc0m);
 
-  string *rec_names = rec_name_list.to_array();
-  string *rec_types = rec_type_list.to_array();
-  void **rec_pointers = rec_ptr_list.to_array();
-  const int rec_num = rec_name_list.length();
-  FileSchema schema(rec_num, rec_types, rec_names);
-  HDFFile *hdf_file = HDFFile::from_schema(schema, file_name, H5F_ACC_TRUNC);
+  string *rec_names = rec_name_list->to_array();
+  string *rec_types = rec_type_list->to_array();
+  void **rec_pointers = rec_ptr_list->to_array();
+  const int rec_num = rec_name_list->length();
+  FileSchema *schema = new FileSchema(rec_num, rec_types, rec_names);
+  HDFFile *hdf_file = HDFFile::from_schema(*schema, file_name, H5F_ACC_TRUNC);
   for (int ri = 0; ri < rec_num; ri++)
     hdf_file->write(rec_names[ri], rec_types[ri], rec_pointers[ri]);
   hdf_file->close();
   
   // Clean memory
+  delete rec_name_list;
+  delete rec_type_list;
+  delete rec_ptr_list;
   delete[] dc0m;
   delete[] rec_names;
   delete[] rec_types;
   delete[] rec_pointers;
+  delete schema;
   delete hdf_file;
 }
 
diff --git a/src/libnptm/Parsers.cpp b/src/libnptm/Parsers.cpp
index 22ff56db..6c8326f7 100644
--- a/src/libnptm/Parsers.cpp
+++ b/src/libnptm/Parsers.cpp
@@ -21,19 +21,20 @@
 
 std::string *load_file(std::string file_name, int *count = 0) {
   std::fstream input_file(file_name.c_str(), std::ios::in);
-  List<std::string> file_lines = List<std::string>();
+  List<std::string> *file_lines = new List<std::string>();
   std::string line;
   if (input_file.is_open()) {
     getline(input_file, line);
-    file_lines.set(0, line);
+    file_lines->set(0, line);
     while (getline(input_file, line)) {
-      file_lines.append(line);
+      file_lines->append(line);
     }
     input_file.close();
   } else {
 	  throw FILE_NOT_FOUND_ERROR;
   }
-  std::string *array_lines = file_lines.to_array();
-  if (count != 0) *count = file_lines.length();
+  std::string *array_lines = file_lines->to_array();
+  if (count != 0) *count = file_lines->length();
+  delete file_lines;
   return array_lines;
 }
-- 
GitLab