Reputation: 464
Does an atomic variable, 'containing' a pointer, take ownership of the pointer?
Consider the following snippet:
{
std::atomic<Foo*> bar(new Foo());
}
// `bar' went out of scope, did it delete pointer to instance of Foo?
Of course I could derive and delete it myself, or work around it in another way; but that's not the point.
What is the defined behavior here, if any?
Upvotes: 1
Views: 1125
Reputation: 171263
No. std::atomic<T*>
has a trivial destructor that does nothing, so it could not delete anything if it owned it.
std::atomic<int>
doesn't "own" the integer, it just stores a value, and similarly std::atomic<int*>
just stores a value, with no ownership or freeing implied.
Upvotes: 2
Reputation: 9841
No, the only thing std::atomic
guarantees is that the object will be free from data races. So you will have free the memory that the pointer points to your self.
If you want a managed dynamic memory container, then either use a unique_ptr or shared_ptr.
Upvotes: 5