How do I get a destructor on an object in a vector not to throw a failed assertion?

  allegro5, assertion, c++, exception

I’m programming a Breakout game in C++. I’m having a HUGE problem that’s preventing me from giving the game multi-ball functionality. I think it has something to do with the destructor. Have a look:

for loop for the balls (Driver.cpp):

for (Ball& b : balls) { // Loops over all balls
    (...)
    // Collision for when you miss
    if (b.getYPos() > HEIGHT) { // If the ball falls below the defined height of the screen
    balls.erase(balls.begin() + b.getID()); // Wipe the ball out of memory to make room (Troublesome line)
    Ball::addToID(-1); // Shift the ball ID to assign to the next ball back one
    (...)
}

And I get this error:

Debug Error!
Program: Breakout.exe
abort() has been called
(Press Retry to debug the application)

I tried a debugging technique that assigned unique sequential IDs (the ID variable) to every ball spawned to test if the order had anything to do with the crashes. It turns out the game only crashes if I drop the balls out of reverse order (Example: If there are 3 balls, dropping ball 1 or ball 0 before ball 2 will crash).

I tried taking away the for loop entirely, and replacing it with a standard for loop, but then the game would throw a failed assertion if a ball even loads into memory at all!

Do you know why this mysterious crash is happening? Or more importantly, a fix for it?

Source: Windows Questions C++

LEAVE A COMMENT