I want to implement an object pool
, so I need to manage the memory of the object myself. So I thought of customizing the deleter of shared_ptr to realize object recycling. Below is an example ref to link
template <typename T>
class object_pool
{
std::shared_ptr<tbb::concurrent_bounded_queue<std::shared_ptr<T>>> pool_;
public:
object_pool()
: pool_(new tbb::concurrent_bounded_queue<std::shared_ptr<T>>()){}
// Create overloads with different amount of templated parameters.
std::shared_ptr<T> create()
{
std::shared_ptr<T> obj;
if(!pool_->try_pop(obj))
obj = std::make_shared<T>();
// Automatically collects obj.
return std::shared_ptr<T>(obj.get(), [=](T*){pool_->push(obj);});
}
};
question
Since the std::shared_ptr's deleter does not release the memory, So how do I release the memory in the "~object_pool" function?