Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2004-04-28 10:50:21


Rozental, Gennadiy wrote:

> There is another counter to direct counter using (I just think out loud -
> it may be that you ok): what if I do not save all of the shared_ptr to the
> same object? What will happened once they will get restored? Wouldn't it
> cause memory leak?

> I do realize that any other solution would be more complicated, but I
> think we should go for it.

I don't really understand what's being said here but I can shed a little
more light on the way serialization is implemented right now. It's also used
as a case study in the documentation and described in detail.

> what if I do not save all of the shared_ptr to the same object?

Here is a scenario:

a) a shared_ptr exists and it points to and object which is pointed by 9
other shared_ptrs.

b) we create and archive and save some objects. The set objects saved only
includes 5 of the shared_ptrs out of the possible 10.

c) later when we load, we create 5 new shared_ptr and one new object.
Object tracking guarantees that only one shared object is created. As each
new shared pointer is loaded, the shared count is incremented. So the new
shared pointer is not identical to the original - its equal to 5.

d) the process (almost?) guarantees that all the shared_ptr are valid in
that they all point to shared object and the shared_count is maintained to
be the correct number. The intention is that this process be exception safe
- I'm not sure that this intention has been realized.

A fuller explanation can be found at:
http://www.rrsd.com/boost/libs/serialization/doc/shared_ptr.html

Robert Ramey.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk