From c6715caaeadea636ca9cdb0f273406dc4d914498 Mon Sep 17 00:00:00 2001
From: Guillaume Pasero <guillaume.pasero@c-s.fr>
Date: Fri, 29 Mar 2019 14:32:33 +0100
Subject: [PATCH] BUG: #1845: no mpi init when testenv is present

---
 Modules/IO/TestKernel/src/otbTestDriver.cxx   |  1 +
 .../src/otbApplicationLauncherCommandLine.cxx | 19 ++++++++++---------
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/Modules/IO/TestKernel/src/otbTestDriver.cxx b/Modules/IO/TestKernel/src/otbTestDriver.cxx
index 3cc8444bbe..930c1910b4 100644
--- a/Modules/IO/TestKernel/src/otbTestDriver.cxx
+++ b/Modules/IO/TestKernel/src/otbTestDriver.cxx
@@ -200,6 +200,7 @@ int Execute(int argc, char * argv[])
   itksysProcess_Execute(process);
   itksysProcess_WaitForExit(process, nullptr);
   int retCode = itksysProcess_GetExitValue(process);
+  itksysProcess_Delete(process);
   return retCode;
 }
 
diff --git a/Modules/Wrappers/CommandLine/src/otbApplicationLauncherCommandLine.cxx b/Modules/Wrappers/CommandLine/src/otbApplicationLauncherCommandLine.cxx
index 8cdca28487..816e56d81f 100644
--- a/Modules/Wrappers/CommandLine/src/otbApplicationLauncherCommandLine.cxx
+++ b/Modules/Wrappers/CommandLine/src/otbApplicationLauncherCommandLine.cxx
@@ -264,12 +264,6 @@ void ShowUsage(char* argv[])
 
 int main(int argc, char* argv[])
 {
-  #ifdef OTB_USE_MPI
-  otb::MPIConfig::Instance()->Init(argc,argv);
-  #endif
-
-  otb::ConfigurationManager::InitOpenMPThreads();
-
   if (argc < 2)
   {
       ShowUsage(argv);
@@ -299,15 +293,22 @@ int main(int argc, char* argv[])
       }
     }
 
-  typedef otb::Wrapper::CommandLineLauncher LauncherType;
-  LauncherType::Pointer launcher = LauncherType::New();
-
   if (vexp.empty())
   {
     ShowUsage(argv);
     return EXIT_FAILURE;
   }
 
+  #ifdef OTB_USE_MPI
+  if (std::find(vexp.begin(), vexp.end(), "-testenv") == vexp.end() )
+    otb::MPIConfig::Instance()->Init(argc,argv);
+  #endif
+
+  otb::ConfigurationManager::InitOpenMPThreads();
+
+  typedef otb::Wrapper::CommandLineLauncher LauncherType;
+  LauncherType::Pointer launcher = LauncherType::New();
+
   bool success = launcher->Load(vexp) && launcher->ExecuteAndWriteOutput();
 
   // shutdown MPI after application finished
-- 
GitLab