Vector erase() & delete then push_back cause crash

Hey!

I´m trying to push new objects into a vector, that works. But when the object dies and I want to remove it something happens because after that if i try to spawn another object the program crashes.

C_Player.cpp
1
2
3
4
5
6
void C_Player::ShootSmall()
{

    C_Entity::EntityList.push_back(new C_MMProj(X,Y, Direction));

}


C_Application.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    std::vector<C_Entity*>::iterator it;
    for(it = C_Entity::EntityList.begin(); it != C_Entity::EntityList.end();)
    {
        if ((*it)->Dead == true)
        {
            delete * it;
            it = C_Entity::EntityList.erase(it);


        }
        else
        {
            it++;
        }
    }


Could someone pleeeeease tell me what is wrong? :(
As I said, the program seems to crash when I try to add a new object after another object has been destroyed.
I don't see anything wrong with the code you have posted, so the problem probably lies somewhere else.

What is the error you are getting when it crashes?
I have a file that I have used to narrow the problem down, and it´s always the same.

"entity died"
"entity died"
"entity died"
"trying to create new entity"

crash. I don´t get it, I thought this would work but it does not. It works as long as I don´t kill off any of the entities.

I can´t really give more information than this, I can only guess this is somehow crashing the program since it worked flawless before I added this.

Oh and the program terminates with status 3.
Last edited on
I'd suggest going through with a debugger and seeing what the state of the program is before the crash occurs.
Never used a debugger before so I guess that will be pretty hard. But if you wanna give some pointers as to how it´s done I could try.


Debug stuff:
http://i.imgur.com/554I4.jpg
http://i.imgur.com/W0Z4c.jpg

That´s all I see and does not tell a noob like me much :/
Last edited on
It's actually quite simple (in theory). Basically, you can set a 'breakpoint' somewhere in your code so that when the program reaches that spot, it pauses and lets you see what all the variables and such are at that point. Then you can step line by line through the code and see where crash-causing values are coming from.
Ok so I got another thing now when i Debugged.

http://i.imgur.com/dykz0.jpg

EntityB got some really weird values.

It´s collision checking between 2 entities I guess, I don´t know why it´s even running that since nothing is colliding.
If nothing is colliding (or it least shouldn't be) I'd step up the call stack to see where the collision function is being called so you can see why it is being called.
I totally removed the collision (with comments) and it still crashes, but now it gives me other stuff on the watch list.

http://i.imgur.com/dDTGO.jpg

This is frustrating..
Why is it crashing? Look at the state of the variables and the crash message and then figure out why/how the data is getting into the state in the first place.
I actually solved it now, and it was just out of pure luck. I had a Cleanup function call by mistake in a Class method that got called in the loop if the object was dead.

Thanks for the help tho :) <3
Topic archived. No new replies allowed.