On Saturday 05 September 2009, Igor R wrote:
Hello,
In my project I've got intrusively ref.counted objects (particularly, ATL COM-objects), which listen to signals of some asynchronous subsystem. Currently, when these objects connect to the signals, they bind their own intrusive smart-ptrs to the slot functor, like this: // pseudo code Object::init() { asyncSubsystem_.onSignal_.connect(bind(&Object::handleSignal, intrusive_from_this...)); // ensure the slot wouldn't bound to a dead object }
Of course, this implies that the object cannot "die" until it's explicitly disconnected from the signal, so when the user of my library wants to get rid of such an object, he must call one more function (say, obj->close()) before resetting his smart-ptr to the object -- which is not just inconvenient, but also threatens the exception safety of user's code.
If you can access the value of the reference count, you could have your slot check to see if the reference count has dropped to 1 (assuming the slot takes the reference-counted pointer by reference). If the use count is one, the slot could disconnect itself.