[geos-devel] Memory leak in Polygon::getArea()?
strk
strk at keybit.net
Fri Feb 27 12:38:33 EST 2004
Do people here think Geometry should define getCoordinatesRO() returning
a const *CoordinateList which does not need to be deleted even if this
would be slower then the corresponsing explicit copy of getCoordinates()
for anything other then LineString* ? Or instead change Polygon::holes
to a LinearRing vector ? Martin ?
mprieto wrote:
> Hi all!
>
> I've downloaded geos-cvs this week and I've found what seems a memory leak. Currently Polygon::getArea() calls LineString::getCoordinates() but that memory is never deleted.
>
> I've changed Polygon::getArea() in the following way
>
> /**
> * Returns the area of this <code>Polygon</code>
> *
> *@return the area of the polygon
> */
> double Polygon::getArea() const {
> double area=0.0;
> CoordinateList *s=shell->getCoordinates();
> area+=fabs(CGAlgorithms::signedArea(s));
> delete s;
> for(unsigned int i=0;i<holes->size();i++) {
> CoordinateList *h=(*holes)[i]->getCoordinates();
> area-=fabs(CGAlgorithms::signedArea(h));
> delete h;
> }
> return area;
> }
>
> and it works correctly for me.
For LineStrings and descendents there is a getCoordinatesRO() function
which returns an read-only pointer to internal structure.
I've changed CGAlgorithms::signedArea signature to accept a
const *CoordinateList and used a call to shell->getCoordinatesRO().
For the holes, this is a bit tricky, because the holes elment of the
polygon object is a vector of Geometry, not LinearRing (even if this
is always the case), and Geometries do not define a getCoordinatesRO()
function. I used your version here ...
--strk;
More information about the geos-devel
mailing list