Is it necessary for UB to move-build an object through the placement of new?
Let's say I have this code:
class Foo { public: Foo() { foo_ = new int; } ~Foo() { delete foo_; } Foo(Foo &&f) { foo_ = std::swap(f.foo_, foo_); } private: int* foo_; } void bar() { void* pMem = malloc(sizeof(Foo)); Foo f1;
If this is not obvious, the f1 member foo_ will be undefined after creating the second foo by placing new and move construction (this undefined value comes from the uninitialized Foo f2 foo_ inside its move constructor, because the values are changed )
Thus, when exiting the bar () area, f1 destructor will try to delete the invalid (uninitialized) pointer.
c ++ c ++ 11 placement-new move-constructor
Bogdan ionitza
source share