[Gdal-dev] importFromWkt() problem

Frank Warmerdam warmerdam at pobox.com
Tue Sep 5 20:05:07 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.
> 
> 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 );
> }

Petteri,

I believe that OSRNewSpatialReference() creates a spatial reference
with a starting reference count of one.  So the more correct approach would
be:


while ( TRUE )
{
    ... // Create WKT
    OGRSpatialReferenceH hRef = OSRNewSpatialReference( NULL );  // <---
    OGRGeometryH hGeomNew;
    OGRErr err = OGR_G_CreateFromWkt( &pszWkt, hRef, &hGeomNew );
    OSRRelease( hRef ); /**** ADDED THIS ****/
    OGR_F_SetGeometryDirectly( hDstFeature, hGeomNew );
    OGR_L_CreateFeature( hDstLayer, hDstFeature )
    OGR_F_Destroy( hDstFeature );
}

But honestly, I'm not sure why you are even creating an empty
spatial reference for the use of these geometries.  You can just pass
NULL in for the OGRSpatialReferenceH in OGR_G_CreateFromWkt() if you
don't have a need to attach spatial reference system to a geometry.

And if you did have a coordinate system, creating it once outside the
loop is far better than creating one for each geometry.

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    | President OSGeo, http://osgeo.org




More information about the Gdal-dev mailing list