c++ - Unwanted destructor call in std::swap -
i've run problem std::swap. have swap object. object releases memory in destructor. i've written move constructor , move assignment operator copies pointer memory. default constructor sets pointer null.
of course, have regular copy constructor , assignment operator, allocate , copy memory, not want swap operation.
when call std::swap, creates temporary object _left using move constructor. then, uses move assignment operator move _right _left, , finally, moves temp object _right.
this looks when bottom of std::swap. however, when step out of bottom of it, destructor temp object runs, freeing memory _right object expecting have.
what's normal accepted way this? hoping avoid having write swap functions since that's point of move constructors/move assignment operators. have use own swap() avoid this?
the move operation should leave object being moved in destructible state, may different state how came move.
if understanding problem right sounds object's move-ctor needs set pointer(s) in object being moved other values came in with. subsequent dtor on now-moved objects should leave memory once referred alone.
Comments
Post a Comment