[Gdal-dev] importFromWkt() problem

Mateusz Loskot mateusz at loskot.net
Tue Sep 5 16:50:33 EDT 2006


Petteri Packalen wrote:
> Mateusz, Frank and others,
> 
>> Are you running your code on Windows with DLLs? If you
>> are, then may be you're dealing with "passing objects
>> across DLL boundaries" issue.
> 
> I rewrote this part of the code using OGR C interface and now it works
> perfectly using both approaches I presented previously. Probably it was
> some sort of dll/heap issue. I use precompiled dll from FWTools under
> Windows.

If you're creating/destroying objects across DLL boundaries,
it's recommended to use dedicated destroy/create factory methods
from OGRGeometryFactory:
http://www.gdal.org/ogr/classOGRGeometryFactory.html

and CreateFeature/DestroyFeature from OGRFeature class:
http://www.gdal.org/ogr/classOGRFeature.html

instead of applying new/delete operators directly.

> I also noticed that there was a substantial memory leak in my code. I
> was creating new OGRSpatialReference within a loop which browses through
> features:
> 
> while ( TRUE )
> {
>    ... // Create WKT
>    OGRSpatialReferenceH hRef = OSRNewSpatialReference( NULL );  // <---
>    OGRGeometryH hGeomNew;
>    OGRErr err = OGR_G_CreateFromWkt( &pszWkt, hRef, &hGeomNew );
>    OGR_F_SetGeometryDirectly( hDstFeature, hGeomNew );
>    OGR_L_CreateFeature( hDstLayer, hDstFeature )
>    OGR_F_Destroy( hDstFeature );
> }
>
> Thus I instantiated very many OGRSpatialReferences. When I instantiated
> only one OGRSpatialReference out of the loop memory leak disappeared (or
> alternatively passing directly NULL to OGR_G_CreateFromWkt as spatial
> reference). I look at the source code and tried to trace why all of
> OGRSpatialReferences are not de-allocated via ~OGRFeature() -->
> ~OGRGeometry() --> OGRSpatialReference::Release() but I didn't follow
> without modifying source code (I use FWTools dll). Probably reference
> counting in OGRSpatialReference cannot handle this kind of misuse of the
> OGR?

SRS object is a shared object and its lifetime is managed by reference
counter. If you have a number of geometries with the same SRS, then you
don't need to instantiate separate SRS object for every geometry,
but share common SRS instance between all your geometries.

However, I'm not sure I see where is the miususe when having
separate SRS for every geometry.

> Anyway, it was my mistake. I really appreciate your help in this list.

You're welcome.

Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the Gdal-dev mailing list