[geos-devel] Memory leak in RelateComputer?

Mateusz Loskot mateusz at loskot.net
Tue May 6 03:45:49 EDT 2008

王永杰 wrote:
> Intend to know how to release the pointer return by Geometry::Relate,
> I trace the source code to class RelateComputer. There is a private
> member variable named "im" initialized in the constructor.
> RelateComputer::RelateComputer(std::vector<GeometryGraph*> *newArg):
> 	arg(newArg),
> 	nodes(RelateNodeFactory::instance()),
> 	im(new IntersectionMatrix())
> {
> }
> The variable "im" is created by operator "new", so I think it must
> destroyed in the destructor, but there is nothing in the destructor of
> RelateComputer.
> RelateComputer::~RelateComputer()
> {
> }
> I think "delete im; im = NULL" is needed, right?


You are correct that RelateComputer allocates new IntersectionMatrix 
object, though the computer instance does not own it.
Next, ownership of the intersection matrix is transfered to a 
RelateComputer caller according to following scheme:

vool foo()
   RelateComputer rc;
   IntersectionMatrix* im = rccomp.computeIM(); // ownership transfer
   bool res = im->matches(...);
   delete im; // required deallocation
   return res;

RelateComputer is not required to deallocate the matrix in its 
destructor, and no memory leak should not occur.

Mateusz Loskot

More information about the geos-devel mailing list