Boost logo

Boost :

From: Joe Gottman (jgottman_at_[hidden])
Date: 2004-11-18 19:57:15


"Peter Petrov" <ppetrov_at_[hidden]> wrote in message
news:loom.20041118T102318-791_at_post.gmane.org...
> Gennadiy Rozental <gennadiy.rozental <at> thomson.com> writes:
>
>> Here we do very simple (or complex,depends on point of view) trick
>> instead
>> of above
>> Let say we have somewhere map<T*,shared_ptr<T>*> registry;
>>
>> if( registry[raw_ptr] == 0 ) {
>> t = shared_ptr<T>( raw_ptr )
>> registry.add( raw_ptr, &t );
>> }
>> else
>> t = *registry[raw_ptr];
>>
>> > // current shared_ptr implementation depends upon an internal
>> > pointer
>> to
>> > a shared count.
>> > }
>>
>> Do I miss something important?
>
> What happens when some time after deserialization, one of the deserialized
> shared_ptr's is destroyed? Your "registry" has no way to know that and
> will
> still hold a shared_ptr referencing the same object, which is not correct.
>

   Maybe we can do this instead:

  map<T*,weak_ptr<T> > registry;
    weak_ptr<T> &registered = registry[raw_ptr];
     t = registered.lock();
     if (!t) {
        t = shared_ptr<T>(raw_ptr);
        registered = t;
     }

After all, one of the main uses for weak_ptr is to implement a cache of
shared_ptr's.

Joe Gottman


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