That's the answer I was looking for. Sorry for my mangled example, but you had the idea... Nat Goodspeed wrote:
Elli Barasch wrote:
Shared pointer question:
[snip odd example]
Will this cause a memory leak? Do I need to do a delete in between, or does the act of reassignment cause the prior object to be dereferenced?
A second assignment to a given shared_ptr should not cause a leak. You should avoid an explicit delete.
If p is a shared_ptr pointing to a heap object, then assigning a new value to p will decrement the previously-referenced object's refcount, which may cause the heap object to be deleted.
This document: http://boost.org/libs/smart_ptr/shared_ptr.htm#Members says this under 'assignment':
shared_ptr & operator=(shared_ptr const & r); // never throws template<class Y> shared_ptr & operator=(shared_ptr<Y> const & r); // never throws template<class Y> shared_ptr & operator=(std::auto_ptr<Y> & r);
Effects: Equivalent to shared_ptr(r).swap(*this).
Returns: *this.
Notes: The use count updates caused by the temporary object construction and destruction are not considered observable side effects, and the implementation is free to meet the effects (and the implied guarantees) via different means, without creating a temporary.
But the important point is that the assignment must behave as though a shared_ptr holding p's prior value has been deleted. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users