Boost logo

Boost Users :

Subject: [Boost-users] how to work with shared pointers to avoid clumsiness
From: Roman Shmelev (rshmelev_at_[hidden])
Date: 2008-12-03 05:50:44


Hi,

I've tried to read boost docs and some forums but still cannot
understand how to make things simple and right.

1) I have an object that is dynamically allocated and I want it to
delete itself (so I understood I need to use shared_ptr)
2) The object must be deleted not after the last shared_pointer to it
will be destroyed, because some external functions will call it later
one more time. And then it must be destroyed.
3) Of course, that external functions cannot accept shared_ptrs.
4) So, I need my object to know when all shared_ptrs to it are
destroyed. Then it sets some flag (I_CAN_DESTROY_MYSELF_SOON) and when
some external code (that holds simple pointer to this object) will
call its function, then it will destroy itself.

How to solve this in an elegant way?
I'm currently do not use shared_ptr, but my implementation is cumbersome.

Maybe I need one more shared_ptr inside the object.. I don't know.

Thank you.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net