Shared_ptr with event queues
So I just recently decided to switch to smart pointers in my multithreaded client. I am currently having problems since pointers(shared_ptr) through the event queue. (By this I mean user events). I did try using a shared_ptr pointer, but that gave me a segmentation fault when I had another shared_ptr inside the shared_ptr structure. Here's an example on what I mean since it sounds a bit confusing:
event.user.data1 = &
Bitmap> bitmap = *((
13// This is where I get a segmentation fault because of the shared_ptr inside the Bitmap structure.
My main question that I am asking is, what is a way to pass a smart pointer through a event queue? If the best way is to use a shared_ptr pointer like I did, could someone please explain how I could fix the segmentation fault?
The entire purpose of a shared_ptr is to destroy the object when all pointers to it are destroyed. As a rule, you should never take the address (pointer to) a smart pointer. That defeats the purpose. I don't think you're going to be able to use a smart pointer for this without negating the purpose of having a smart pointer in the first place. You're unfortunately going to have to manage the memory yourself.
The internal shared_ptr is fine (Bitmap::bitmap), but the stack-allocated smart pointer variable will be destroyed as soon as it goes out of scope. As a consequence, it will also destroy the Bitmap inside of it, and the Bitmap::bitmap inside of that.
Eh you're breaking my heart I was hoping for a 1 line fix
I'll look it over in the morning and look for other options.
Thread #608659. Printed from Allegro.cc
Well, a shared pointer will just not work here, at least not inside of the event itself.
Something more like :
Your ALLEGRO_BITMAP* will be temporarily unavailable to destroy until you retrieve the event itself.