[geos-devel] How to destroy PreparedGeometry object?

Martin Davis mbdavis at refractions.net
Mon Aug 11 19:00:31 EDT 2008


Ugh.

Sounds like we need a 
PreparedGeometryFactory::destroyPreparedGeometry(PreparedGeometry *) 
method, then - as per your suggestion.

Does this same situation exist for all objects which might be allocated 
on the heap?  There's lots of classes in GEOS "below the Geometry level" 
which might be used by a coder using the C++ API.  Perhaps there should 
be a warning about this (in the non-existent FAQ?)

M



Mateusz Loskot wrote:
> Martin Davis wrote:
>> I would think that there should be a destructor defined for the 
>> PreparedGeometry class.  Would this be the appropriate C++ pattern?
>
> Martin,
>
> Not really. The C++ standard says:
>
> "If a class has no user-declared destructor, a destructor is
> declared implicitly"
>
> It means, PreparedGeometry class has implicit destructor.
> The problem is that it is never called (implicitly) because 
> PreparedGeometryFactory constructs PreparedGeometry objects on heap.
> So, user has to trigger destructor execution. In most cases, we do in 
> C++ this:
>
> PreparedGeometryFactory f;
> Geometry* g = ... // create geometry
> PreparedGeometry* pg = f.create(g);
> ... // use prepared geom
> delete pg; // request destruction
>
> And I could use this pattern without problems, however it is not 
> bullet proof for "DLL Hell" problems on Windows/Visual C++ because 
> "memory crosses DLL boundaries". In example above, GEOS DLL binary 
> allocates memory for prepared geometry, but client calls "delete" 
> operator on side of his program (.exe binary or another .dll). And 
> crash is very probable.
>
> GeometryFactory::destroyGeometry solves this problem because it calls 
> delete operator on the GEOS DLL side.
>
> The same pattern is used in GDAL:
>
>
> "How should I deallocate resources acquainted from GDAL on Windows?"
>
> http://trac.osgeo.org/gdal/wiki/FAQMiscellaneous
>
>
> Best regards,

-- 
Martin Davis
Senior Technical Architect
Refractions Research, Inc.
(250) 383-3022



More information about the geos-devel mailing list