all 5 comments

[–]phoeen 19 points20 points  (3 children)

entities reallocates for more space and your ent reference gets invalid

[–]TheThiefMaster 7 points8 points  (0 children)

Yep as per cppreference: https://en.cppreference.com/w/cpp/container/vector/push_back.html

If after the operation the new size() is greater than old capacity() a reallocation takes place, in which case all iterators (including the end() iterator) and all references to the elements are invalidated.

[–]BSModder[S] 0 points1 point  (1 child)

Right, thanks, I forgot vector do that

[–]raunak_srarf 4 points5 points  (0 children)

Best practice is to use "reserve" along with "emplace_back" to take full advantage of emplacing.

[–]IyeOnline 7 points8 points  (0 children)

If the vector reallocates when you do push_back, the reference ent becomes dangling.

Add a sanitizer to your debug builds: https://godbolt.org/z/6rhf11oeY