0

Hi guys I'm having a problem in my Ogre problem. I'm not sure if this is the best place to ask this but I may as well. Here is the sample of the code I made in order to create a 2D array of enemies(for a space invaders game

    for(int i = 0; i < 5; i++) //Manages the YPOS coordinate of the enemy
    {
        for(int j = 0; j < 5; j++) //Manages the YPOS coordinate of the enemy
        {
            stringstream ss;
            ss << j;
            std::string pos = ss.str();
            ss.clear();
            ss << i;
            pos += "," + ss.str();
            std::string enemyName = "Enemy " + pos;
            Ogre::Entity * enemyEnt = mSceneMgr->createEntity(enemyName, "razor.mesh");
            Ogre::SceneNode *node1 = mSceneMgr->getRootSceneNode()->createChildSceneNode    (enemyName+"ParentNode");
            Ogre::SceneNode *node2 = node1->createChildSceneNode(enemyName+"Node");
            enemyEnt->setMaterialName("Examples/Chrome");
            mSceneMgr->getSceneNode(enemyName+"Node")->attachObject(ent);
            int multiplier = 100;
            if(j < 3)
            {
                multiplier *= -1;
            }
            if(j == 3)
            {
                multiplier = 0;
            }
            Vector3 initialPos;
            initialPos.x = (j+1) * multiplier;
            initialPos.y = 0;
            initialPos.z = 3000 - ((i+1) * multiplier);
            enemyVec.push_back(new Enemy(mSceneMgr,node2, initialPos, j, i, 200 ));
        }
    }
    enem->setEnemies(enemyVec);
}

The following is there error I'm getting

Unhandled exception at 0x59a6ad4e (msvcp100d.dll) in C00146012 Project - 3D Space Invaders.exe: 0xC0000005: Access violation reading location 0xcccccd24.

When debugging it brings it to this segment of code in xutility

#if _ITERATOR_DEBUG_LEVEL == 2
if (_Myproxy != 0)
{   // proxy allocated, drain it
    _Lockit _Lock(_LOCK_DEBUG);

for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter; *_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)
    (*_Pnext)->_Myproxy = 0;
    _Myproxy->_Myfirstiter = 0;
    }
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */

However I know the error is in the line:

enem->setEnemies(enemyVec);

Any and all help would be greatly appreciated :)

AzKai
  • 317
  • 1
  • 2
  • 12
  • Can you tell us which is line 128? – Floris Aug 22 '13 at 14:53
  • (Let's suppose) I have images turned off in my browser. How the heck am I going to know the error message? –  Aug 22 '13 at 14:54
  • 2
    It isn't related to your problem, but your stringstream operations can be done in 2 lines instead of 7. And I don't see where `ent` is declared. Is that supposed to say `enemyEnt`? – Zac Howland Aug 22 '13 at 14:55
  • Sorry @h2CO3 - I tried to be helpful and replaced the link with the embedded image... Overzealous editing? I will put it back. – Floris Aug 22 '13 at 14:55
  • Are you expecting `ss.clear()` to rid the stream of its contents? – chris Aug 22 '13 at 14:55
  • @H2C03: I had posted a link until someone edited it and put up the image. The link is: http://i.stack.imgur.com/94bJN.png – AzKai Aug 22 '13 at 14:56
  • @AzKai, And the image in that link is no exception to his point. – chris Aug 22 '13 at 14:56
  • 1
    Well, isn't it a good thing when a whole array of enemies crashes at creation? – lapk Aug 22 '13 at 14:58
  • 1
    Constructive feedback would be nice...... @Zac Howland: You're absolutely correct. I had forgotten to edit that and now the error has changed. I will re-edit my image to reflect the new one. – AzKai Aug 22 '13 at 14:59
  • @Floris Nope, the edit was perfectly fine. Inline screenshots are better than links to screenshots, so thanks for the edit. I actually wanted OP to post the error message as text **instead** of the screenshot. –  Aug 22 '13 at 15:00
  • @AzKai Here's some: please copy-paste the **textual** error message instead of a screenshot. –  Aug 22 '13 at 15:01
  • How are `enemisVec` and 'setEnemies` declared? – Zac Howland Aug 22 '13 at 15:20
  • set enemies is a void function that sets the value of enemyVec by accapting a parameter std::vector newVec enemyVec is declared as: std::vector enemyVec; – AzKai Aug 22 '13 at 15:24
  • I think you need to show the code of `setEnemies`; and is there some way you can check that your `enemyVec` contains (only) valid objects, and that those are the only elements that `setEnemies` works with? – Floris Aug 22 '13 at 16:39
  • whats enem? whats ent? and whats the value od pos? especially, for what your just Declaring thoose Entitys automatic in loop scope? – dhein Aug 23 '13 at 08:00

1 Answers1

0

What ever you want to do with the Entitys and Scene nodes, in this line

    enem->setEnemies(enemyVec);

remember that you declared the Nodes and the Entity just in the scope of the loop, and as you didnt made a full copy of them and pasted it into allocated memory, your vector is probably refering to the automatic allocated nodes out of their lifetime and whatever you are going to do with them, its undefined behaviour. So probably some states are still the same in their memory, and Ogre can work with them, but gets errors, because its not exactly that what its supposed to be.

And even if Ogre is handling this, you should notify that

for each

i;j == 3

the initial position is the same. So it could be as refering to the Ogre error, that if pos has anything to do with initialPos, you have non unique strings, where unique strings are expected.

dhein
  • 6,431
  • 4
  • 42
  • 74