Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Variable may be unused.
[simgrid.git] / src / smpi / internals / smpi_global.cpp
index bd0b4e0..e75aef9 100644 (file)
@@ -13,6 +13,7 @@
 #include "src/smpi/include/smpi_actor.hpp"
 #include "xbt/config.hpp"
 
+#include <algorithm>
 #include <cfloat> /* DBL_MAX */
 #include <dlfcn.h>
 #include <fcntl.h>
@@ -426,14 +427,18 @@ typedef void (*smpi_fortran_entry_point_type)();
 
 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;
-  char* name = argv[0];
-  char* instance = argv[1];
+  // copy C strings, we need them writable
+  std::vector<char*>* args4argv = new std::vector<char*>(args.size());
+  std::transform(begin(args), end(args), begin(*args4argv), [](const std::string& s) { return xbt_strdup(s.c_str()); });
+
+#if !SMPI_IFORT
+  // take a copy of args4argv to keep reference of the allocated strings
+  const std::vector<char*> args2str(*args4argv);
+#endif
+  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);
@@ -448,13 +453,9 @@ static int smpi_run_entry_point(smpi_entry_point_type entry_point, std::vector<s
 #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;
+  for (char* s : args2str)
+    xbt_free(s);
+  delete args4argv;
 #endif
 
   if (res != 0){