From 8505256710f653c8a38530087ee017723ea67cfc Mon Sep 17 00:00:00 2001
From: Otmane Lahlou <otmane.lahlou@c-s.fr>
Date: Fri, 9 Sep 2011 16:48:31 +0200
Subject: [PATCH] ENH : avoid segfaults when the input filename is changed
 after a first execution of the application

---
 Code/Core/otbWrapperApplication.cxx            |  3 ++-
 Code/Core/otbWrapperOutputImageParameter.cxx   | 18 +++++++++++++++---
 Code/Core/otbWrapperOutputImageParameter.h     |  2 ++
 .../otbApplicationLauncherQt.cxx               |  3 ++-
 4 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/Code/Core/otbWrapperApplication.cxx b/Code/Core/otbWrapperApplication.cxx
index 2b7d9f17ec..4cb75d1189 100644
--- a/Code/Core/otbWrapperApplication.cxx
+++ b/Code/Core/otbWrapperApplication.cxx
@@ -121,6 +121,7 @@ void Application::ExecuteAndWriteOutput()
       {
       Parameter* param = GetParameterByKey(*it);
       OutputImageParameter* outputParam = dynamic_cast<OutputImageParameter*>(param);
+      outputParam->InitializeWriter();
       m_CurrentProcess = outputParam->GetWriter();
       outputParam->Write();
       m_WroteOutput++;
@@ -368,6 +369,7 @@ void Application::SetParameterOutputImage(std::string parameter, VectorImageType
   if (dynamic_cast<OutputImageParameter*>(param))
     {
     OutputImageParameter* paramDown = dynamic_cast<OutputImageParameter*>(param);
+    paramDown->InitializeWriter();
     paramDown->SetValue(value);
     }
 }
@@ -560,7 +562,6 @@ Application::GetExecuteProgress()
       res = m_CurrentProcess->GetProgress();
     }
 
-  
   return res;
 }
 
diff --git a/Code/Core/otbWrapperOutputImageParameter.cxx b/Code/Core/otbWrapperOutputImageParameter.cxx
index 0a666534e5..e278b85524 100644
--- a/Code/Core/otbWrapperOutputImageParameter.cxx
+++ b/Code/Core/otbWrapperOutputImageParameter.cxx
@@ -33,14 +33,26 @@ OutputImageParameter::~OutputImageParameter()
 {
 }
 
+
+void
+OutputImageParameter::InitializeWriter( )
+{
+  m_Writer = WriterType::New();
+}
+
 void
 OutputImageParameter::Write( )
 {
+  if(!m_Writer.IsNotNull())
+    {
+    itkExceptionMacro(<< "The Writer is null, please call explicitly the OutputImageParameter::InitializeWriter() method");
+    }
+
   if (m_Image.IsNotNull())
     {
-      m_Writer->SetInput(m_Image);
-      m_Writer->SetFileName(this->GetFileName());
-      m_Writer->Update();
+    m_Writer->SetInput(m_Image);
+    m_Writer->SetFileName(this->GetFileName());
+    m_Writer->Update();
     }
 }
 
diff --git a/Code/Core/otbWrapperOutputImageParameter.h b/Code/Core/otbWrapperOutputImageParameter.h
index 101c9a3af3..fb990ff15b 100644
--- a/Code/Core/otbWrapperOutputImageParameter.h
+++ b/Code/Core/otbWrapperOutputImageParameter.h
@@ -68,6 +68,8 @@ public:
 
   void Write();
 
+  void InitializeWriter();
+
 protected:
   /** Constructor */
   OutputImageParameter();
diff --git a/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx b/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx
index e7082bace1..50e7dafd79 100644
--- a/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx
+++ b/Code/Wrappers/ApplicationLauncherQt/otbApplicationLauncherQt.cxx
@@ -90,6 +90,7 @@ int main(int argc, char* argv[])
 
   // Create GUI based on module
   QtWidgetView* gui = new QtWidgetView(app);
+  gui->CreateGui();
 
   // Connect the View "Quit" signal, to the mainWindow close slot
   QObject::connect(gui, SIGNAL(QuitSignal()), mainWindow, SLOT(close()));
@@ -107,7 +108,7 @@ int main(int argc, char* argv[])
   mainWindow->setCentralWidget(gui);
   mainWindow->addDockWidget(Qt::BottomDockWidgetArea, qdock);
   
-  gui->CreateGui();
+  // Show the main window
   mainWindow->show();
 
   // Start event processing loop
-- 
GitLab