boost::bind and shared pointers

I have this problem. Im using boost::bind for programming the call-back mechanism for a gui system and to pass messages (possibly, delayed messages). My problem is, that when i bind member functions, if i specify the instance of the class as a shared_ptr, then i get memory leaks because the binding's shared_ptr to the instance keeps it alive, and it potentially produces cyclic references that in the current model, would be complicated to break. And of course, if i use raw pointers to the binding instances, then i'll get a crash if the instance has been destroyed and i try to execute the binding function. So what i would need, is to use bind with weak pointers, and just do nothing if locking it fails. I'm not sure how to do that, or how to assemble a good walkaround. I'd like it to be as automatic as possible. Thanks for your time, i appreciate any help :)

Alejandro Martínez wrote:
So what i would need, is to use bind with weak pointers, and just do nothing if locking it fails.
I'm not sure how to do that, or how to assemble a good walkaround. I'd like it to be as automatic as possible.
I think you should look at the thread_safe_signals library.

Alejandro Martínez
I have this problem.
Im using boost::bind for programming the call-back mechanism for a gui system and to pass messages (possibly, delayed messages).
My problem is, that when i bind member functions, if i specify the instance of the class as a shared_ptr, then i get memory leaks because the binding's shared_ptr to the instance keeps it alive, and it potentially produces cyclic references that in the current model, would be complicated to break.
And of course, if i use raw pointers to the binding instances, then i'll get a crash if the instance has been destroyed and i try to execute the binding function.
So what i would need, is to use bind with weak pointers, and just do nothing if locking it fails.
I'm not sure how to do that, or how to assemble a good walkaround. I'd like it to be as automatic as possible.
Thanks for your time, i appreciate any help :)
If you want boost::bind to work with weak_ptr then just add the following
code before using bind.
#include
participants (3)
-
Alejandro Martínez
-
Nat Goodspeed
-
Roman Perepelitsa