0

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

Community
  • 1
  • 1
  • When is this iterator created? Are you inserting into the vector *after* creating the iterator? If this is the case, the iterator is not guaranteed to be valid. – AndyG Dec 05 '13 at 21:14
  • Be careful not to delete your objects (and still have reference/pointers to them), you might want to take a look at reference counting – Sebastian Hoffmann Dec 05 '13 at 21:16
  • @AndyG Nothing is added to the vectors after the iterator is created. @Paranaix Do the `Enemy` objects that I make in `spawnEnemy` get destroyed at the end of the function? If this happens, why does the `Enemy` still get drawn by the `SceneNode` graph? – user3051585 Dec 05 '13 at 21:20
  • Why do you dereference like this **iter...shouldnt it just be *iter – Josh Engelsma Dec 05 '13 at 21:21
  • @JoshEngelsma: *iter will return a pointer to an enemy, **iter will return the actual object. – AndyG Dec 05 '13 at 21:21
  • yes, but do you have to dereference the pointer to enemy? – Josh Engelsma Dec 05 '13 at 21:23
  • @JoshEngelsma: Have to? No, well maybe. If you want. Point is, if the OP wants to mess with the enemy object by dereferencing the pointer returned from a dereferenced iterator, s/he can. It's not illegal or (inherently) dangerous. – AndyG Dec 05 '13 at 21:25
  • @AndyG you're right, I was just wondering why you would want too...agreed, not illegal – Josh Engelsma Dec 05 '13 at 21:26
  • @user3051585: We are going to need more code. At least show us how you are iterating through, and maybe some more logic. Ideally, a small compileable example that fully recreates your bug would be nice, but I understand that may be a big undertaking in this case. – AndyG Dec 05 '13 at 21:33
  • @AndyG I've posted my other question which has more code relating to where `itr` gets called, but I believe you already found it earlier. I don't think I can recreate this problem in a small program, but I will try. – user3051585 Dec 05 '13 at 21:48
  • @user3051585: Can enemies die? What happens in code when they do? Do you simply call `delete` on the pointer? Do you make sure you unattach the child from its parent by calling `erase` on the parent's child vector? Do you make sure you call `erase` on mEnemies after calling `delete`? If you don't delete the memory, what do you do with the pointers? Change a state variable to read DEAD or something? I think this information would be very useful. – AndyG Dec 05 '13 at 21:52
  • @AndyG In this build, enemies cannot die, and for debugging purposes I set the game to only spawn one. The seg fault happens the frame that the first enemy is spawned (which, strangely enough is NOT the first time **itr gets called, so perhaps these enemies are working, in which case I guess this code would be working properly. My main question was making sure that `world.mEnemies` and the node graph are both enemy pointers that point to the same data, and to make sure I don't accidentally destroy that Enemy data somewhere in my code.) – user3051585 Dec 05 '13 at 21:57

0 Answers0