Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
marcoperic opened this issue Mar 7, 2022 · 3 comments

Comments

@marcoperic
Copy link

marcoperic commented Mar 7, 2022

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 marcoperic changed the title Crashing ("Debug assertion failed. C++ vector subscript out of range") when removing colony with low population Crashing ("Debug assertion failed! Expression: vector subscript out of range") when removing colony with low population Mar 7, 2022
@marcoperic marcoperic changed the title Crashing ("Debug assertion failed! Expression: vector subscript out of range") when removing colony with low population Crashing ("Debug assertion failed! Expression: vector subscript out of range") when removing colony with population of 0. Mar 7, 2022
@marcoperic
Copy link
Author

marcoperic commented Mar 7, 2022

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
Copy link
Author

marcoperic commented Mar 7, 2022

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

@johnBuffer
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants