[Gdal-dev] Memory leak in GeoTIFF driver

Eric Dönges Eric.Doenges at rcs.ei.tum.de
Tue Jan 4 09:04:04 EST 2005


Hi,

I've found a memory leak in the GeoTIFF driver in the 
GTiffDataset::SetGCPs member
function - any GCPs already defined when SetGCPs is called are not 
freed; neither is
the pointer to the projection string. Both are simply overwritten with 
the new values.
The attached patch should correct both problems.

On a related note, is there any reason why the GeoTIFF driver will only 
load GCPs if
there are more than 6 GCPs defined in the file ? I think the driver 
should simply
load whatever is available, and let the application worry about wether 
there are
enough points to do anything useful with.

With kind regards,
Eric

-------------- next part --------------
--- frmts/gtiff/geotiff.cpp.orig	Tue Jan  4 14:39:51 2005
+++ frmts/gtiff/geotiff.cpp	Tue Jan  4 14:54:15 2005
@@ -3759,8 +3759,23 @@
 {
     if( GetAccess() == GA_Update )
     {
+        /* If we already have GCPs defined, free those first. */
+        if( this->nGCPCount > 0 )
+        {
+                for( int i = 0; i < this->nGCPCount; i++ )
+                        CPLFree( this->pasGCPList[i].pszId );
+
+                CPLFree( this->pasGCPList );
+        }
+    
 	this->nGCPCount = nGCPCount;
 	this->pasGCPList = GDALDuplicateGCPs(nGCPCount, pasGCPList);
+
+        /*
+         * NOTE: this->pszProjection should always point to valid memory
+         * (though the projection string itself might be empty, i.e. ""
+         */
+        CPLFree( this->pszProjection );
 	this->pszProjection = CPLStrdup( pszGCPProjection );
         bGeoTIFFInfoChanged = TRUE;
 
-------------- next part --------------









More information about the Gdal-dev mailing list