Reputation: 504
I have a std::vector
of GameObject
s. I access these GameObject
s using direct pointers to them. (Don't worry about the pointers getting invalidated, I update the pointers when the vector
realocates itself). The GameObject
class has a function called load()
.
So, in one case, I call the load()
of an instance of GameObject
which is stored within the vector
. This function pushes another GameObject
at the back of the vector
. This causes the vector
to run out of memory and reallocate.(or resize). Now, then that call to push_back
returns, the load()
is not able to access any of the GameObject
instance's members any more. The instance still exists on the same vector though.
Why is the function not able to access the members? The members seen by the load
contain junk/incorrect values. Any explanations?
Upvotes: 1
Views: 41
Reputation: 179819
vector
can't do magic. It has to move elements, either by using their move constructors (if possible) or copy constructors, and then destroy the old objects in the previous location.
So, in your case, GameObject::~GameObject
will be called for the object on which you called GameObject::load
. Sure, there's another logically identical object elsewhere, but your this
pointer is invalid when load
returns. There's no way that vector
can update your this
pointer while a call is in progress.
Upvotes: 2