[geos-devel] Smart Pointers solve all MM problems?

Ferdinando Villa fvilla at zoo.uvm.edu
Thu May 29 09:13:30 EDT 2003


On Thu, 2003-05-29 at 08:32, Norman Vine wrote:
> > I believe there is a 
> > well-known problem with reference counting not handling circular 
> > chains of pointers. 

I'll have to look into this when I'm back - I also remember this but I
couldn't find the problem mentioned in the docs I have, so I imagine
it's solved in general (myself., I always use tree structures, so if
there's a problem it's likely I haven't run into it). Anyway ref
counting isn't the only way to implement smart pointers although it is
the most popular - Alexandrescu has a great discussion (and I don't
remember seeing the circular pointer thing mentioned there).
 
> > Also, am I right in thinking that smart pointers don't help you 
> > if you have functions which alloc memory structures and return 
> > them? (Which happens all over JTS - e.g. in indexes and edge 
> > graph structures).
> 
> Long lived objects can be handled specially or just use the ref count 
> bumping trick
> 

If the only new operator you write is within the constructor of a smart
pointer (i.e. the function that allocates the memory returns a smart
pointer) then the copy constructor of the receiving memory bumps the
reference count, and you only lose the object if you don't assign the
return value (which is the correct behavior). The only thing to remember
is: never write a new outside a smart pointer constructor. Then it's
almost impossible to go wrong. Of course you need to use the right kind
of smart pointer - for long-lived object you don't use auto_ptr, but one
of the 'shared' family.

> -- 
> ferdinando villa, ph.d.                    associate professor
> gund institute for ecological economics, university of vermont
> 590 main street, burlington, vt 05405    phone: (802) 656-2972




More information about the geos-devel mailing list