Nasty vector trouble!

Started by
2 comments, last by Zomgbie 13 years, 4 months ago
Hello people!

In my game, i use one vector to store all my objects. At max, there might be around 100 objects contained within the vector, so it's not that crowded. But! I've noticed an issue while adding objects to my vector. Sometimes, when adding many objects right after eachother, my game crashes. After a little debugging, i traced the crash down to this function:

	baseObject			*object;	vector<baseObject *>::iterator	collisionIndex, endVector;	/* Set the end */	endVector = this->lists.end();	/* Iterate over all objects in the list */	for (this->listsIndex = this->lists.begin(); this->listsIndex < endVector; this->listsIndex++) {		/* Assign the object */		object = *this->listsIndex;		/* Check for collision */		for (collisionIndex = this->lists.begin(); collisionIndex < endVector; collisionIndex++) {			/* We can't collide with our selfs */			if (object == *collisionIndex)				continue;			/* Check for collision THIS IS WHERE IT CRAHES */			object->defaultCollision(*collisionIndex);		}	}


When a bullet collides with an enemy, it creates an explosion of particles. What i thought, was that since the particles got added to the same list i'm allready iterating through, something nasty would happen and crash. You may see that i've tried to fix this by setting the endVector before going through my list (I push_back() objects), and thus not accessing the new objects in this iteration. But that did not fix it.

So i am out of ideas. Help, please? :)
Omg, zombie! Zomgbie.
Advertisement
"What i thought, was that since the particles got added to the same list i'm allready iterating through, something nasty would happen and crash"

While Iterating you assign a Pointer to the current Position and also add things to your Vector, did I understand that correct?

Adding things to your Vector can mean that a whole new block of Memory is assigned to that vector and simply "moved" there. If your still using a pointer to the old location you'll get your crash.
Quote:Original post by Zomgbie
What i thought, was that since the particles got added to the same list i'm allready iterating through, something nasty would happen and crash.
My guess is that the vector reallocates its internal storage (because vector<>.size() == vector<>.capacity()) so your pointers and iterators are all invalidated.
Quote:Original post by m1o1d1
"What i thought, was that since the particles got added to the same list i'm allready iterating through, something nasty would happen and crash"

While Iterating you assign a Pointer to the current Position and also add things to your Vector, did I understand that correct?

Adding things to your Vector can mean that a whole new block of Memory is assigned to that vector and simply "moved" there. If your still using a pointer to the old location you'll get your crash.


Yes, you got that correct.

Okey, i see. Should i perhaps queue the adding and run those commands as soon as im not iterating? Or is there a smarter solution? :)
Omg, zombie! Zomgbie.

This topic is closed to new replies.

Advertisement