AntSimulator icon indicating copy to clipboard operation
AntSimulator copied to clipboard

Crashing ("Debug assertion failed! Expression: vector subscript out of range") when removing colony with population of 0.

Open marcoperic opened this issue 3 years ago • 3 comments

Edit: Confirmed when using the callback that you wrote for removing a colony (hitting "remove" in GUI), it will sometimes crash if trying to remove a colony with a population of 0.

In the update() function in Simulation, I have a check to see if any colonies have a population of 0 ants. If there are 0 ants, removeColony() is called and the colony is removed from the world, as well as being removed under the hood, from the colonies vectors in Simulation, world, and renderer.

Strangely, it also only appears to happen when using colonies with somewhat large starting populations (>50?). If you try testing with small colony sizes, it may not occur. Additionally, I believe I have only one instance of crashing when using the actual "remove" button in the GUI (compared to crashes happening every single time otherwise).

It seems like it is either a problem with the index_vector.hpp class, or maybe the target field in Ant ...? I have been investigating this problem all day with no luck. I'll make a comment below that goes into greater detail.

marcoperic avatar Mar 07 '22 09:03 marcoperic

In simulation's update() function, I have a check at the very bottom (below checkForFights and updateColoniesStats) that checks to see if any colonies are extinct. It looks like this: for (Colony& colony : colonies) { if (colony.eliminated) removeColony(colony.id); }

eliminated is a bool that is triggered when the ant population is equal to zero.

When debugging, I see that the error originates from removeColony(uint8_t colony_id) and occurs in the for loop with c.stopFightsWith(colony_id);`

From there, the next error takes place in the Colony class in the stopFightsWith function, inside of the for loop, at if(a.target) { Where a.target seems to be the problem. Here is the call stack:

image

Note that the first four lines have line numbers that should match with yours. Everything below operator bool() Line 359 probably differs from your code.

marcoperic avatar Mar 07 '22 09:03 marcoperic

My thoughts: I don't think anything I added to your code managed to cause this problem. I think the problem comes from trying to iterate over the Ants of a colony that no longer exists (was deleted because population was 0). Maybe there is a problem with Ant.target when the colony is extinct, when there are no ants, or when the colony no longer exists?

In any case, debugging keeps pointing to lines of code involving if (ants.target), even when I try to patch this bug.

Let me know if there is anything I can do to help!

Regards, Marco

marcoperic avatar Mar 07 '22 09:03 marcoperic

Hello, Thank you for your detailed analysis, I will look into this

johnBuffer avatar Mar 08 '22 10:03 johnBuffer