Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Another try to fix memory leaks in smpi_run_entry_point.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 10 Aug 2018 18:04:40 +0000 (20:04 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Fri, 10 Aug 2018 18:04:40 +0000 (20:04 +0200)
src/smpi/internals/smpi_global.cpp

index 2285daf..0fa4260 100644 (file)
@@ -13,6 +13,7 @@
 #include "src/smpi/include/smpi_actor.hpp"
 #include "xbt/config.hpp"
 
 #include "src/smpi/include/smpi_actor.hpp"
 #include "xbt/config.hpp"
 
+#include <algorithm>
 #include <cfloat> /* DBL_MAX */
 #include <dlfcn.h>
 #include <fcntl.h>
 #include <cfloat> /* DBL_MAX */
 #include <dlfcn.h>
 #include <fcntl.h>
@@ -426,16 +427,16 @@ typedef void (*smpi_fortran_entry_point_type)();
 
 static int smpi_run_entry_point(smpi_entry_point_type entry_point, std::vector<std::string> args)
 {
 
 static int smpi_run_entry_point(smpi_entry_point_type entry_point, std::vector<std::string> args)
 {
-  int argc_saved = args.size();
-  int argc=argc_saved;
-  char** argv = new char*[argc + 1];
-  for (int i = 0; i != argc; ++i)
-    argv[i] = xbt_strdup(args[i].c_str());
-  argv[argc] = nullptr;
-#if !SMPI_IFORT
-  char* name = argv[0];
-  char* instance = argv[1];
-#endif
+  // copy C strings, we need them writable
+  std::vector<char*> args4argv(args.size());
+  std::transform(begin(args), end(args), begin(args4argv), [](const std::string& s) { return xbt_strdup(s.c_str()); });
+
+  // take a copy of args4argv to keep reference of the allocated strings
+  const std::vector<char*> args2str(args4argv);
+  int argc = args4argv.size();
+  args4argv.push_back(nullptr);
+  char** argv = args4argv.data();
+
   simgrid::smpi::ActorExt::init(&argc, &argv);
 #if SMPI_IFORT
   for_rtl_init_ (&argc, argv);
   simgrid::smpi::ActorExt::init(&argc, &argv);
 #if SMPI_IFORT
   for_rtl_init_ (&argc, argv);
@@ -449,15 +450,9 @@ static int smpi_run_entry_point(smpi_entry_point_type entry_point, std::vector<s
 
 #if SMPI_IFORT
   for_rtl_finish_ ();
 
 #if SMPI_IFORT
   for_rtl_finish_ ();
-#else
-  for (int i = 0; i != argc; ++i)
-    xbt_free(argv[i]);
-  if (argc_saved > 3) {
-    xbt_free(instance);
-    xbt_free(name);
-  }
-  delete[] argv;
 #endif
 #endif
+  for (char* s : args2str)
+    xbt_free(s);
 
   if (res != 0){
     XBT_WARN("SMPI process did not return 0. Return value : %d", res);
 
   if (res != 0){
     XBT_WARN("SMPI process did not return 0. Return value : %d", res);