Hello!
I have some shared pointer shared_ptr<T> pointer1(new T(1));
.
Now, in some other part of code I have an explicit copy of pointer2
(guess it would be stored in a std::map
or some other container). Let's say that copy was done like map.insert(make_pair(key1, pointer1));
.
I am using that second copy only to precache some data and this means that if the main pointer is already invalid, there is no need to store the second pointer. What should I do in this case?
Is there any way to force the memory deallocation for the second pointer if I know that pointer1
became invalid in some other part of my code?
Or should I take the ugly way - from time to time check my map for pointers which have ptr.unique()
set to true
and destruct them?
Maybe some alternatives / advices?
Edit - plain code sample
std::map<int, shared_ptr<int> > map;
{
shared_ptr<int> pointer1(new int(5));
map.insert(std::make_pair(0, pointer1));
}
Is there any way / trick to make map contain <0, shared_ptr[NULL]>
instead of <0, shared_ptr[5]>
after these operations happen?
Thanks