Steps I took to debug:
1) Run the program, select option '5' from the menu to generate a random map
2) Program crashes, popup box asks if I want to debug, I say yes
3) VS breaks on the line of offending code, which is in some internal-to-VS source file (stdthrow.cpp).
4) Using the call stack viewer in VS, I backtrack up to the line that is actually calling a standard function.
5) Call stack takes me to here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
void map::get_random_coord(int &x, int &y)
{
int size = height*width;
int num = 0;
do{
num = rand() % size; //Generate random number
x = ((num%width)-1); //Convert to x
y = (num/width); // Convert to y
}while(x==0 || x==(width-1) || y==0 || y==(height-1));
while(grid[y][x].game_piece != NULL){ // <<<<<<<<<<<<<<<<< THIS LINE
if(grid[y][x].game_piece != NULL)
get_random_coord(x, y);
}
return;
}
|
6) I put 'y' and 'x' in the watch window so I can see their values. y=17 and x=-1
7) I immediately recognize that -1 is an invalid index for a vector.
8) I look at how x is calculated, it seems you are subtracting 1 from it (why?)
9) I remove the -1, rebuild, rerun. It no longer crashes (at least not when I pick that option)
The only other way I could get it to crash (that I tried) was to type "exit" at the main menu, rather than selecting one of the available options. How did you get it to crash in beast_manager.cpp?