[gdal-dev] Memory leaks when using SQL queries with shapefiles

Pierluigi Guasqui guasqui at actgate.com
Wed Jul 11 07:28:11 PDT 2012


I am experiencing a big memory allocation (with no subsequent memory 
release) when using SQL queries with shapefiles. This is a sample code 
that triggers this problem:


     // opening shapefile data source
     const char *fname = "path/to/shapefile.shp";
     OGRDataSource *poDS;
     poDS = OGRSFDriverRegistrar::Open( fname, FALSE );

     if( poDS == NULL )
         fprintf( stderr, "%s: could not open input file\n", fname );
         return -1;

     // creating the spatial filter
     OGRLineString regionPolygon;
     OGRRawPoint *pointsList = new OGRRawPoint[5];
     pointsList[0].x = -180; pointsList[0].y =  90;
     pointsList[1].x =  180; pointsList[1].y =  90;
     pointsList[2].x =  180; pointsList[2].y = -90;
     pointsList[3].x = -180; pointsList[3].y = -90;
     pointsList[4].x = -180; pointsList[4].y =  90;
     regionPolygon.setPoints(5, pointsList);
     delete[] pointsList;

     // executing the query
     const char *query = "SELECT * FROM table_name WHERE some_field LIKE 
'%bla%' ORDER BY some_other_field";
     OGRLayer *rs = poDS->ExecuteSQL( query, &regionPolygon, NULL );

     // releasing memory
     poDS-> ReleaseResultSet( rs );
     OGRDataSource::DestroyDataSource( poDS );

With a 100.000 records shapefile "ExecuteSQL" has allocated over 300 
MBytes of memory with no deallocation when calling "ReleaseResultSet".

The memory leak is triggered only when "ExecuteSQL" is called with a 
spatial filter (even trivial as shown in the above code) and the SQL 
query contains a 'WHERE' clause and an 'ORDER BY' clause. Is this a bug?

I am using GDAL 1.9.1 under Windows platform.

Any help would be really appreciated!
Thank you,

Pierluigi Guasqui
Applied Coherent Technology Corp.

More information about the gdal-dev mailing list