Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a non-owning shared_ptr?

I am pretty new to C++11 and am now working on improving my C++ skills by trying to avoid direct usage of pointers. I am trying to write a sprite manager that keeps track of previously loaded sprites and frees unused ones. I am trying to use shared_ptr (pointer to the bitmap) for this, but the manager also has to keep a shared_ptr to create the sprites with - so the reference count doesn't drop to 0. Can I somehow declare the "parent" shared_ptr in my manager non-owning so it doesn't count as a reference (and still create owning copies of that shared_ptr)?

like image 783
Cubic Avatar asked Jul 30 '12 12:07

Cubic


1 Answers

Use a weak_ptr. That will solve your problem. You won't need to free them as they will be automatically freed. Use a lock on the weak_ptr to get an actual shared_ptr.

The use_count will also give you the current number of references.

like image 104
ThirdOne Avatar answered Sep 19 '22 12:09

ThirdOne