Roman Shmelev wrote:
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.
You could use a custom deleter function that, instead of deleting the object, calls a method that sets the "I_CAN_DESTROY_MYSELF_SOON" flag. Of course you're risking memory leaks if the expected function call never occurs. -- Jon Biggar jon@floorboard.com jon@biggar.org jonbiggar@gmail.com