[Gdal-dev] Free OGRGeometry?
Frank Warmerdam
warmerdam at pobox.com
Fri Nov 25 14:38:53 EST 2005
On 11/25/05, Craig Miller <craig.miller at spatialminds.com> wrote:
> I'm trying to set a spatial filter, but am having difficulty determining how
> I am supposed to create and free OGRGeometry correctly. In ogr2ogr the
> spatial filter doesn't appear to be freed. In my code, this is causing a
> memory leak. What is the best way to allocate/deallocate OGRGeometry?
>
> In ogr2ogr it done like this:
>
> // inside of main()
> // Declared
> OGRGeometry *poSpatialFilter = NULL;
>
> // Initialized
> OGRLinearRing oRing;
>
> oRing.addPoint( atof(papszArgv[iArg+1]), atof(papszArgv[iArg+2]) );
> oRing.addPoint( atof(papszArgv[iArg+1]), atof(papszArgv[iArg+4]) );
> oRing.addPoint( atof(papszArgv[iArg+3]), atof(papszArgv[iArg+4]) );
> oRing.addPoint( atof(papszArgv[iArg+3]), atof(papszArgv[iArg+2]) );
> oRing.addPoint( atof(papszArgv[iArg+1]), atof(papszArgv[iArg+2]) );
>
> poSpatialFilter = new OGRPolygon();
> ((OGRPolygon *) poSpatialFilter)->addRing( &oRing );
>
> // Used
> if( poSpatialFilter != NULL )
> poLayer->SetSpatialFilter( poSpatialFilter );
>
> // Freed?
Craig,
The most direct way is "delete poSpatialFilter;" after
the SetSpatialFilter() call. The SetSpatialFilter() method
will copy the geometry it is passed. You could also just
put the OGRPolygon on the stack.
Finally, there is now a SetSpatialFilterRect() method on the
OGRLayer that looks like:
virtual void SetSpatialFilterRect( double dfMinX, double dfMinY,
double dfMaxX, double dfMaxY );
Along with simplicity, the other advantage of calling this method is
that it will be certain to recognise the special case for a bounding
rectangle query (as opposed to a more complex polygon intersection).
Best regards,
--
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush | Geospatial Programmer for Rent
More information about the Gdal-dev
mailing list