In this code, I want to create an Enemy
object, and push a pointer of it to two different places: as a child in the tree of all my drawable objects (Enemy
is drawable), and to a list of Enemy*
's called mEnemies
that keeps track of all active Enemy
objects, without having to iterate through my graph.
void World::spawnEnemy(float x, float y, EnemyType::ID type, IState::ID state)
{
Enemy* enemy(new Enemy(mTextures, type, state));
enemy->setPosition(x, y);
mEnemies.push_back( enemy );
mSceneLayers[Air]->attachChild( enemy );
}
When my code tries to draw the graph of drawable nodes, it succeeds with the correct position as specified in the above function. However, when I create an iterator auto itr
for mEnemies
and try to access (**itr)
, then the debugger says that it is an Enemy
with completely nonsensical values, and I get a seg fault when I try to access.
For referance, the attachChild
code looks like this:
void SceneNode::attachChild(SceneNode* child)
{
child->mParent = this;
mChildren.push_back(child);
}
Other related code is in my other question: Cannot figure out Seg Fault, lots of detail provided