On Apr 7, 2008, at 11:00 AM, Joseph Thomson wrote:
Actually, I think I have a better way which uses auto_ptr (or the boost ptr
containers) as you suggested.  It still isn't thread safe, but this isn't of
immediate concern, and there are other methods for this anyway.

Before, I was storing a map of shared_ptrs in the ObjectManager and a
weak_ptr reference in any ObjectHandles to check if the object is still
valid.  Instead:

- A map of auto_ptrs (or a ptr_map of Objects) would be stored in the
ObjectManager.
- On its creation, Object would create and store a shared_ptr<ObjectHandle>,
passing a raw pointer to it in the process (don't worry, this is still
safe).
- Any user wanting to get access to the Object would request an ObjectHandle
from ObjectManager, which would in turn request a copy of Object's
shared_ptr<ObjectHandle>.
- On its destruction (by ObjectManager), Object would inform its
ObjectHandle of its destruction, and ObjectHandle would set an internal flag
to indicate that it is expired.
- ObjectHandle would handle any requests from the user by first checking the
expiry flag, and then forwarding the call to Object using its stored raw
pointer.  The expiry flag is set by the Object itself, so there is no chance
of the pointer being invalid when called.

Any advice/thoughts are appreciated.

Cheers,

Joe

Joe,

Suppose that: (a) ObjectManager uses shared_ptrs, not auto_ptrs, 
and (b) the base class of all Objects derives from boost::tr1::enable_shared_from_this.

Then suppose the logic of ObjectHandle was something like:

shared_ptr<Object> ptr = Object->shared_from_this();
if (ptr) {
ptr->perform-requested-action();
}
// shared ptr is reset when it goes out of scope


Wouldn't this:
>> preserve the single point of deletion (since the shared_ptr above is local).
>> be thread-safe (because 'ptr' prevents the object from being deleted until the operation is complete.

Or am I overlooking something really obvious in my (perhaps overly-)quick response?

Cheers,

Rick