From fe26b79a15155637ec24acf743e7bff9f097e89e Mon Sep 17 00:00:00 2001
From: Sebastien Dinot <sebastien.dinot@c-s.fr>
Date: Fri, 22 Jun 2012 00:14:46 +0200
Subject: [PATCH] BUG: Segfault with some tests on Debian

---
 Code/Testing/CMakeLists.txt    | 10 +++++-----
 Code/Testing/otbTestHelper.cxx | 31 +++++++++++++++----------------
 Code/Testing/otbTestMain.h     |  4 +++-
 3 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/Code/Testing/CMakeLists.txt b/Code/Testing/CMakeLists.txt
index 4068f6bb22..08d1e0c4e1 100644
--- a/Code/Testing/CMakeLists.txt
+++ b/Code/Testing/CMakeLists.txt
@@ -2,8 +2,8 @@
 # Sources of non-templated classes.
 FILE(GLOB OTBTesting_SRCS "*.cxx" )
 
-# Remove the otbTestDriver cause only an executable is nedded 
- LIST(REMOVE_ITEM OTBTesting_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/otbTestDriver.cxx" )
+# Remove the otbTestDriver cause only an executable is nedded
+LIST(REMOVE_ITEM OTBTesting_SRCS "${CMAKE_CURRENT_SOURCE_DIR}/otbTestDriver.cxx" )
 
 
 ADD_LIBRARY(OTBTesting ${OTBTesting_SRCS})
@@ -11,7 +11,7 @@ ADD_LIBRARY(OTBTesting ${OTBTesting_SRCS})
 #    PROPERTIES
 #    LINK_INTERFACE_LIBRARIES ""
 #)
-TARGET_LINK_LIBRARIES (OTBTesting OTBBasicFilters OTBIO OTBCommon ITKBasicFilters)
+TARGET_LINK_LIBRARIES (OTBTesting OTBBasicFilters OTBIO OTBCommon ITKBasicFilters OTBOGRAdapters)
 IF(OTB_LIBRARY_PROPERTIES)
   SET_TARGET_PROPERTIES(OTBTesting PROPERTIES ${OTB_LIBRARY_PROPERTIES})
 ENDIF(OTB_LIBRARY_PROPERTIES)
@@ -39,9 +39,9 @@ IF(CMAKE_COMPILER_IS_GNUCXX)
   SET_SOURCE_FILES_PROPERTIES(otbTestDriver.cxx PROPERTIES COMPILE_FLAGS -w)
 ENDIF(CMAKE_COMPILER_IS_GNUCXX)
 
-ADD_EXECUTABLE(otbTestDriver otbTestDriver.cxx) 
+ADD_EXECUTABLE(otbTestDriver otbTestDriver.cxx)
 TARGET_LINK_LIBRARIES(otbTestDriver OTBIO OTBTesting)
-SET(ITK_TEST_DRIVER "${EXECUTABLE_OUTPUT_PATH}/otbTestDriver" 
+SET(ITK_TEST_DRIVER "${EXECUTABLE_OUTPUT_PATH}/otbTestDriver"
     CACHE INTERNAL "otbTestDriver path to be used by subprojects")
 
 IF(NOT OTB_INSTALL_NO_DEVELOPMENT)
diff --git a/Code/Testing/otbTestHelper.cxx b/Code/Testing/otbTestHelper.cxx
index 18c7597dab..44c1e03890 100644
--- a/Code/Testing/otbTestHelper.cxx
+++ b/Code/Testing/otbTestHelper.cxx
@@ -413,18 +413,18 @@ int TestHelper::RegressionTestAsciiFile(const char * testAsciiFileName, const ch
         }
       }
 
-    
+
     // Ignore lines with "RTTI" and "Modified Time" inside by default : to avoid multibaseline
     // between Linux and Win32 plateforms
     if ((!ignoreCurrentLineRef) && (!ignoreCurrentLineTest))
       {
       if(isToBeIgnoredForAnyComparison(strfileref))
         ignoreCurrentLineRef = true;
-      
+
       if(isToBeIgnoredForAnyComparison(strfiletest))
         ignoreCurrentLineTest = true;
       }
-    
+
     //Compare the lines only if none is supposed to be ignored
     //Note: the iterator increment will take care of moving only the
     //ignored one if the order does not matter
@@ -664,7 +664,7 @@ int TestHelper::RegressionTestImage(int cpt, const char *testImageFilename, cons
     typedef otb::PrintableImageFilter<ImageType> RescaleType;
     typedef RescaleType::OutputImageType         OutputType;
     typedef otb::ImageFileWriter<RescaleType::OutputImageType> WriterType;
-    
+
     RescaleType::Pointer rescale = RescaleType::New();
     WriterType::Pointer writer = WriterType::New();
     writer->SetInput(rescale->GetOutput());
@@ -672,15 +672,15 @@ int TestHelper::RegressionTestImage(int cpt, const char *testImageFilename, cons
     std::cout << "<DartMeasurement name=\"ImageError " << cpt << "\" type=\"numeric/double\">";
     std::cout << status;
     std::cout << "</DartMeasurement>" << std::endl;
-    
+
     std::cout << "<DartMeasurement name=\"NumberOfPixelsWithDifferences " << cpt << "\" type=\"numeric/integer\">";
     std::cout << numberOfPixelsWithDifferences;
     std::cout << "</DartMeasurement>" << std::endl;
-    
+
     std::cout << "<DartMeasurement name=\"ToleranceDiffPixelImage\" type=\"numeric/double\">";
     std::cout << toleranceDiffPixelImage;
     std::cout << "</DartMeasurement>" << std::endl;
-    
+
     diff->UpdateOutputInformation();
     ImageType::SizeType size = diff->GetOutput()->GetLargestPossibleRegion().GetSize();
 
@@ -691,12 +691,12 @@ int TestHelper::RegressionTestImage(int cpt, const char *testImageFilename, cons
       try
         {
         rescale->SetInput(diff->GetOutput());
-        
+
         for (unsigned int i = 1; i <= std::min(diff->GetOutput()->GetNumberOfComponentsPerPixel(), 3U); ++i)
           {
           rescale->SetChannel(i);
           }
-        
+
         rescale->Update();
         }
       catch (...)
@@ -712,7 +712,7 @@ int TestHelper::RegressionTestImage(int cpt, const char *testImageFilename, cons
         {
         itkGenericExceptionMacro(<< "Error during write of " << diffName.str());
         }
-      
+
       std::cout << "<DartMeasurementFile name=\"DifferenceImage " << cpt << "\" type=\"image/png\">";
       std::cout << diffName.str();
       std::cout << "</DartMeasurementFile>" << std::endl;
@@ -728,7 +728,7 @@ int TestHelper::RegressionTestImage(int cpt, const char *testImageFilename, cons
 
     baselineReader->UpdateOutputInformation();
     size = baselineReader->GetOutput()->GetLargestPossibleRegion().GetSize();
-    
+
     if( size[0]*size[1] <= m_MaxArea)
       {
       std::ostringstream baseName;
@@ -751,7 +751,7 @@ int TestHelper::RegressionTestImage(int cpt, const char *testImageFilename, cons
         {
         itkGenericExceptionMacro(<< "Error during write of " << baseName.str());
         }
-      
+
       std::cout << "<DartMeasurementFile name=\"BaselineImage " << cpt << "\" type=\"image/png\">";
       std::cout << baseName.str();
       std::cout << "</DartMeasurementFile>" << std::endl;
@@ -767,7 +767,7 @@ int TestHelper::RegressionTestImage(int cpt, const char *testImageFilename, cons
 
     testReader->UpdateOutputInformation();
     size = testReader->GetOutput()->GetLargestPossibleRegion().GetSize();
-    
+
     if( size[0]*size[1] <= m_MaxArea)
       {
       std::ostringstream testName;
@@ -790,7 +790,7 @@ int TestHelper::RegressionTestImage(int cpt, const char *testImageFilename, cons
         {
         itkGenericExceptionMacro(<< "Error during write of " << testName.str());
         }
-      
+
       std::cout << "<DartMeasurementFile name=\"TestImage " << cpt << "\" type=\"image/png\">";
       std::cout << testName.str();
       std::cout << "</DartMeasurementFile>" << std::endl;
@@ -1223,7 +1223,7 @@ int TestHelper::RegressionTestOgrFile(const char *testOgrFilename, const char *b
       // If no verbose and an diff was found, exit checking. The full checking will be executed in verbose mode
       if ((!m_ReportErrors) && (nbdiff != 0)) return (1);
       } //if(nbdiff == 0)
-    
+
     }
 
   /* -------------------------------------------------------------------- */
@@ -1231,7 +1231,6 @@ int TestHelper::RegressionTestOgrFile(const char *testOgrFilename, const char *b
   /* -------------------------------------------------------------------- */
   OGRDataSource::DestroyDataSource( ref_poDS );
   OGRDataSource::DestroyDataSource( test_poDS );
-  OGRCleanupAll();
 
   return (nbdiff != 0) ? 1 : 0;
 }
diff --git a/Code/Testing/otbTestMain.h b/Code/Testing/otbTestMain.h
index fe5c39a04b..09f51154a6 100644
--- a/Code/Testing/otbTestMain.h
+++ b/Code/Testing/otbTestMain.h
@@ -28,6 +28,7 @@
 #include "itkMultiThreader.h"
 #include "itkMacro.h"
 
+#include "otbOGRDriversInit.h"
 #include "otbTestHelper.h"
 
 #include "itkMersenneTwisterRandomVariateGenerator.h"
@@ -87,6 +88,7 @@ int main(int ac, char* av[])
   StringList ignoredLines;
   ignoredLines.clear();
 
+  otb::ogr::Drivers::Init();
   otb::TestHelper::Pointer testHelper = otb::TestHelper::New();
 
   RegisterTests();
@@ -410,7 +412,7 @@ int main(int ac, char* av[])
       return -1;
       }
     std::cout << "-------------  End control baseline tests    -------------" << std::endl;
-    
+
     return result;
     }
 }
-- 
GitLab