[geos-devel] Deleting PrecisionModels
David Blasby
dblasby at refractions.net
Mon Apr 7 17:51:59 EDT 2003
Yury and everyone,
I've been looking at memory management and the PrecisionModel(). Take a
look at Geometry.cpp, GeometryFactory.cpp, WKTReader.cpp, and something
like Point.cpp.
There's some major issues here, I hope that they can be easily solved.
WKTReader/GeometryFactory
Lets look at some code like this:
WKTReader *r = new WKTReader(new GeometryFactory(new
PrecisionModel(),-1));
Geometry g1 = r->read("some WKT");
Geometry g2 = r->read("some more WKT");
At this stage the WKTReader, GeometryFactory (inside the
WKTReader), g1, and g2 all have references to the same PrecisionModel().
If you were to:
a) delete r;
g1,g2, and the GeometryFactory now have pointers to an
illegal PrecisionModel. WKTReader delete method also deletes the shared
PrecisionModel.
b) delete g1;
r, g2, and the GeometryFactory have an illegal PrecisionModel
since the Geometry delete method also deletes the shared PrecisionModel.
c) delete the GeometryFactory
This is okay since the Factory doesnt delete the
PrecisionModel. Note - using "new GeometryFactory()" will leak a
PrecisionModel.
JTS gets away with this because of java's reference counting and garbage
collection.
The main issue is that Geometries share a common PrecisionModel (via the
GeometryFactory). This PrecisionModel is deleted in several places.
One way to deal with this is to *never* delete a PrecisionModel - leave
that up to the user. Typically a program would look like:
{
PrecisionModel *pm = new PrecisionModel(); //only ever make one!!
// make your WKTReader, GeometryFactories, and Geometry's here
and always
// refer to the same pm.
// when there are no more Geometries or Factories, etc...
delete pm;
}
The user would have to be careful to never delete a PrecisionModel or
have the compiler auto-delete it (like at the end of function).
Alternatively, each geometry can have its very own PrecisionModel. When
the Geometry is deleted, also delete the PrecisionModel.
The GeometryFactory would have to clone its PrecisionModel everytime it
creates a geometry. Although this will have every Geometry take up more
space, it would always be freed when its no longer needed.
Comments?
dave
More information about the geos-devel
mailing list