[geos-devel] How to destroy PreparedGeometry object?
Mateusz Loskot
mateusz at loskot.net
Mon Aug 11 18:46:27 EDT 2008
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,
--
Mateusz Loskot, http://mateusz.loskot.net
Charter Member of OSGeo, http://osgeo.org
More information about the geos-devel
mailing list