[Gdal-dev] Free OGRGeometry?

Craig Miller craig.miller at spatialminds.com
Fri Nov 25 16:19:54 EST 2005


Thanks for the reply Frank.  It was very helpful.  :-)

I didn't notice the geometry factory before.  I switched to using it for
both creation and deletion and the heap errors are gone.

The code is living inside a class that handles the cacheing of OGR data.
The new code (for people searching the archives) looks like:


// Member variable declared in header
OGRGeometry *m_pSpatialFilter;

// Inside my classes SetSpatialFilter method
// Later, there will be other SetSpatialFilter methods that allow users to
specify
// filters by regions (which is why there is an OGRGeometry* as a member
variable
void CGeoLayer::SetSpatialFilter(double dMinX, double dMinY, double dMaxX,
double dMaxY)
{
	if (m_pSpatialFilter != NULL)
	{
		delete m_pSpatialFilter;
	}

	OGRLinearRing  oRing;

    oRing.addPoint( dMinX, dMinY );
    oRing.addPoint( dMinX, dMaxY );
    oRing.addPoint( dMaxX, dMaxY );
    oRing.addPoint( dMaxX, dMinY);
    oRing.addPoint( dMinX, dMinY);

    m_pSpatialFilter = OGRGeometryFactory::createGeometry( wkbPolygon);
    ((OGRPolygon *) m_pSpatialFilter)->addRing( &oRing );

	UpdateFeatureCache();
}

// Inside Empty() method called by destructor
if (m_pSpatialFilter != NULL)
{
	OGRGeometryFactory::destroyGeometry( m_pSpatialFilter);
	m_pSpatialFilter = NULL;
}



-----Original Message-----
From: fwarmerdam at gmail.com [mailto:fwarmerdam at gmail.com] On Behalf Of Frank
Warmerdam
Sent: Friday, November 25, 2005 1:00 PM
To: Craig Miller
Subject: Re: [Gdal-dev] Free OGRGeometry?

On 11/25/05, Craig Miller <craig.miller at spatialminds.com> wrote:
> I should have mentioned that using delete (under windows) is causing 
> an unhandled exception.  I thought that there might be a special CPL* 
> routine that has to be used to prevent this from happening.
>
> I'm using a rect now, but in the future I hope to allow other 
> geometric elements to be used to filter the geometry (like an 
> ecological region, county, etc).

Craig,

I imagine your mainline is not build with /MD, but GDAL13.DLL is.
In that case GDAL uses the MSVCRT heap but your application uses its own
private heap. This can result in all sorts of problems.

Since you did the "new OGRPolygon" in your own code, I would have thought
doing the delete there would also be ok, but perhaps not.

There is also an OGRGeometryFactory::destroyGeometry( OGRGeometry *) method
you can use to destroy a geometry within the GDAL DLL.  This is often useful
if you need to destroy a geometry created by GDAL.

Of course, your exception could be due to some other heap corruption issue.
It is hard to know.

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