[geos-devel] Re: Memory leak in RelateComputer?

=?GB2312?B?zfXTwL3c?= wang.yongjie.2008 at gmail.com
Tue May 6 22:44:33 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?
>
>  Wang,
>
>  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.
>
>  Greetings
>  --
>  Mateusz Loskot
>  http://mateusz.loskot.net
>
Mateusz,

First, thanks very much for your reply.
That's great you provide a solution to avoid memory leak in class
RelateComputer. It work very well, but I don't think that idea is good
enough to solve this problem. For example, if I create a
RelateComputer pointer and delete it immediately.

  RelateComputer* rc = new (RelateComputer);
  delete rc;
  rc = NULL;

Does Memory leak ocurr?
need I do this before the pointer destroyed?

  delete rc->computeIM(); // destroy the pointer im.

I don't think that codes are graceful.
So I still consider deallocate the matrix in its destructor is a betty way.
"Who created, who destroyed"

How do you think?

Thank you for your patience.

Best regards to everyone.

--
Wang Yongjie
Tel:              +86(10)8282 5800 Ext. 8411
Fax:             +86(10)8282 5816
Email:          yongjiemail at gmail.com
MSN/Gtalk:  yongjiemail at gmail.com
Blog:            www.iseeker.org/blog


More information about the geos-devel mailing list