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?
Yes,
other._datawill be freed when it goes out of scope (assuming a good destructor of course). However there is one item to consider: Ifother._datarefers 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.