Reputation: 1839
With reference to this MSDN article , at the end (Section: Robust Programming) it states,
To prevent resource leaks, always free resources (such as memory, file handles, and sockets) in the move assignment operator.
What will happen if the move assignment is instead implemented as:
MemoryBlock& operator=(MemoryBlock&& other)
{
if (this != &other)
{
std::swap(_data, other._data);
std::swap(_length, other._length);
}
return *this;
}
Wouldn't the "_data" of the rvalue that "other" references be freed when it goes out of scope?
Upvotes: 3
Views: 397
Reputation: 218780
Yes, other._data
will be freed when it goes out of scope (assuming a good destructor of course). However there is one item to consider: If other._data
refers to a resource that needs a timely destruction, it may be destructed late in this design. An example might be the locked state of a mutex.
Upvotes: 2