emplace_back calls move constructor, and destructor

2.In case of the first instance creation, why the move constructor, then the destructor is called?

Because the insertion of the 2nd element by emplace_back causes the reallocation; the inner storage of the vector needs to be extended, the elements in the old storage have to be copied/moved to the new storage, then destroyed.

You can use reserve in advance to avoid reallocation.

1.Why do I need to define a move constructor in my class if I just want to emplace_back items, and never use push_back.

As the above explanation said, vector needs to move elements by copy/move operation. So you have to define the copy or move constructor for the class. This is true for both emplace_back and push_back, because they both add elements to vector and might cause reallocation.


Odds are the capacity of your vector was one, and when you put the second element in, it had to resize the vector. That can turn into a bunch of things being moved in memory, and the symptoms you see.

Kerreks advice is good. I suggest printing the vectors capacity before and after each operation to see if the capacity change is the cause.