From 5774cb13fd6a586ccf26b9e227fa91c13245658f Mon Sep 17 00:00:00 2001
From: Guillaume Pasero <guillaume.pasero@c-s.fr>
Date: Fri, 29 Nov 2019 10:47:24 +0100
Subject: [PATCH] BUG: fix dangling pointer

---
 Modules/IO/IOGDAL/src/otbGDALImageIO.cxx | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx b/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx
index 56b4c7671e..165b4549ec 100644
--- a/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx
+++ b/Modules/IO/IOGDAL/src/otbGDALImageIO.cxx
@@ -1467,7 +1467,8 @@ void GDALImageIO::InternalWriteImageInformation(const void* buffer)
   else if (gcpCount)
     {
     otbLogMacro(Debug, << "Saving GCPs to file (" << m_FileName << ")")
-    GDAL_GCP* gdalGcps = new GDAL_GCP[gcpCount];
+    std::vector<GDAL_GCP> gdalGcps(gcpCount);
+    std::vector<OTB_GCP> otbGcps(gcpCount);
     for (unsigned int gcpIndex = 0; gcpIndex < gcpCount; ++gcpIndex)
       {
       // Build the GCP string in the form of GCP_n
@@ -1475,7 +1476,7 @@ void GDALImageIO::InternalWriteImageInformation(const void* buffer)
       lStream << MetaDataKey::GCPParametersKey << gcpIndex;
       std::string key = lStream.str();
 
-      OTB_GCP gcp;
+      OTB_GCP &gcp = otbGcps[gcpIndex];
       itk::ExposeMetaData<OTB_GCP>(dict, key, gcp);
 
       gdalGcps[gcpIndex].pszId      = const_cast<char*>(gcp.m_Id.c_str());
@@ -1498,12 +1499,10 @@ void GDALImageIO::InternalWriteImageInformation(const void* buffer)
     std::string gcpProjectionRef;
     itk::ExposeMetaData<std::string>(dict, MetaDataKey::GCPProjectionKey, gcpProjectionRef);
 
-    dataset->SetGCPs(gcpCount, gdalGcps, gcpProjectionRef.c_str());
+    dataset->SetGCPs(gcpCount, gdalGcps.data(), gcpProjectionRef.c_str());
 
     // disable geotransform with GCP
     writeGeotransform = false;
-
-    delete[] gdalGcps;
     }
 
   /* -------------------------------------------------------------------- */
-- 
GitLab