From ee625274d8b413cd706453a643601b0e9f5766e6 Mon Sep 17 00:00:00 2001
From: Julien Malik <julien.malik@c-s.fr>
Date: Wed, 8 Jun 2011 18:32:04 +0200
Subject: [PATCH] BUG: putenv does not copy the string internally, so don't
 rely on it

---
 Code/Core/otbWrapperApplicationFactory.cxx    |  4 +--
 .../otbApplicationLauncherQt.cxx              | 27 ++++++++++---------
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/Code/Core/otbWrapperApplicationFactory.cxx b/Code/Core/otbWrapperApplicationFactory.cxx
index 4ee48b03d4..5d090d0e70 100644
--- a/Code/Core/otbWrapperApplicationFactory.cxx
+++ b/Code/Core/otbWrapperApplicationFactory.cxx
@@ -48,7 +48,7 @@ ApplicationFactory::CreateApplication(const std::string& name)
       }
     else
       {
-      std::cerr << "Error ImageIO factory did not return an ImageIOBase: " << (*i)->GetNameOfClass() << std::endl;
+      otbMsgDevMacro( << "Error ApplicationFactory factory did not return an Application: " << (*i)->GetNameOfClass() << std::endl );
       }
     }
 
@@ -84,7 +84,7 @@ ApplicationFactory::GetAvailableApplications()
       }
     else
       {
-      std::cerr << "Error ImageIO factory did not return an ImageIOBase: " << (*i)->GetNameOfClass() << std::endl;
+      otbMsgDevMacro( "Error ApplicationFactory factory did not return an Application: " << (*i)->GetNameOfClass() << std::endl );
       }
     }
 
diff --git a/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx b/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx
index 1b6e870997..938a5b522a 100644
--- a/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx
+++ b/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx
@@ -19,6 +19,7 @@
 #include "otbWrapperApplication.h"
 #include "otbWrapperApplicationFactory.h"
 #include "otbWrapperQtWidgetView.h"
+#include "itksys/SystemTools.hxx"
 
 using otb::Wrapper::Application;
 using otb::Wrapper::ApplicationFactory;
@@ -44,15 +45,19 @@ int main(int argc, char* argv[])
 
     // Load the path in the environment
     std::string specificEnv("ITK_AUTOLOAD_PATH=");
-    std::list<std::string>::const_iterator it;
-    for (it = modulePathList.begin(); it != modulePathList.end(); ++it)
+    std::list<std::string>::const_iterator it = modulePathList.begin();
+    while( it != modulePathList.end() )
       {
       std::string modulePath = *it;
 
       specificEnv += *it;
-      specificEnv += ":";
+      ++it;
+      if (it != modulePathList.end())
+        specificEnv += ":";
       }
-    putenv (const_cast<char *>(specificEnv.c_str()));
+
+    // do NOT use putenv() directly, since the string memory must be managed carefully
+    itksys::SystemTools::PutEnv(specificEnv.c_str());
     }
 
   // Reload factories to take into account new path
@@ -64,17 +69,13 @@ int main(int argc, char* argv[])
     {
     std::cerr << "Could not find application " << moduleName << std::endl;
 
-    std::cout << "Module search path :" << std::endl;
-    std::list<std::string>::const_iterator it;
-    for (it = modulePathList.begin(); it != modulePathList.end(); ++it)
-      {
-      std::cout << "  " << *it << std::endl;
-      }
+    const char* modulePath = itksys::SystemTools::GetEnv("ITK_AUTOLOAD_PATH");
+    std::cout << "Module search path : " << (modulePath ? modulePath : "") << std::endl;
 
-    std::cout << "Available applications :" << std::endl;
     std::list<std::string> list = ApplicationFactory::GetAvailableApplications();
-    //std::list<std::string>::const_iterator it;
-    for (it = list.begin(); it != list.end(); ++it)
+
+    std::cout << "Available applications : " << (list.empty() ? "None" : "") << std::endl;
+    for (std::list<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
       {
       std::cout << "  " << *it << std::endl;
       }
-- 
GitLab