Opened 6 years ago

Closed 5 years ago

#918 closed defect (fixed)

memory leak in GeometryCollection::getCoordinate()

Reported by: basiliscos Owned by: geos-devel@…
Priority: major Milestone:
Component: Default Version: 3.6.2
Severity: Unassigned Keywords:
Cc:

Description

The implementation is

const Coordinate*
GeometryCollection::getCoordinate() const
{
        // should use auto_ptr here or return NULL or throw an exception !
        //      --strk;
        if (isEmpty()) return new Coordinate();
        return (*geometries)[0]->getCoordinate();
}

So, if a collection is empty it returns coordinate and (to prevent memory leaks) assumes that caller should delete it. Otherwise it returns non-owning Coordinate* and not delete is required from caller.

The best way is return NULL as it is told in the comment.

The alternative will be

...
static Coordinate zero;
if (isEmpty()) return &zero;

Change History (1)

comment:1 by Daniel Baston <dbaston@…>, 5 years ago

Resolution: fixed
Status: newclosed

In d1556af/git:

Fix memory leak on GeometryCollection::getCoordinate

Add tests to ensure all Geometry types have the same behavior when
calling getCoordinate on an empty geometry.

Fixes #918

Note: See TracTickets for help on using tickets.