Is this code valid and safe? Will memory be leaked?
class Widget;
auto local_cache = LocalCache.GetInstance(); // init cache
{
std::shared_ptr<Widget> tmp;
local_cache.GetWidget(&tmp);
// process with tmp
}
GetWidget() looks weird, it is actually a template like GetWidget(T* value) to pass in a value pointer for it to write to, and T is instantiated with shared_ptr<Widget>
void LocalCache::GetWidget(std::string k, std::shared_ptr<Widget>* w_ptr_ptr) {
if (cache_map_ contains k) {
*w_ptr_ptr = ... // copy-assigned
}
}
void LocalCache::PutWidget(std::string k, std::shared_ptr<Widget> v) {
cache_map_.emplace(k, v);
if (reach limit) {
cache_map_.prune();
}
}
void LocalCache::prune() {
// prune the cache
}