"Unhandled exception"

Started by
6 comments, last by Mizipzor 18 years, 9 months ago
When I run my program I get:

"Unhandled exception at 0x7c901230 in SDL.exe: User breakpoint."
I dont really know what the error means but I commented out all the functions called in my main loop and brought them back one by one. So I know its this functions in which the error occurs:

void Game::MakeWorld() {
	sdl.writeLog("Making world");	// write in the log file
	bool LastWasOne = false;

	// loop through the entire tilesystem, 
	// inner loop is for each line on the x axis, and is runned once for every line 
	for(int y = 0; y < sizeX; y++) {
		for(int x = 0; x < sizeY; x++) {
			if(LastWasOne == true){
				SetTileData(x, y, '0');
				LastWasOne = false;
			} 
			else {
				SetTileData(x, y, '1');
				LastWasOne = true;
			}
		}
	}
}


SizeX and Y is how many tiles I got on each axis, they are defined like this:

#define RESX 640
#define RESY 480
#define GRIDSIZE 16

	// let the class know how many tiles we got on each axis
	sizeX = RESX / GRIDSIZE;
	sizeY = RESY / GRIDSIZE;
The SetTileData function writes to a vector which holds a char for every tile on the gamefield;

// calculate how many tiles there is on the x axis multiply with y then add the remaining to find out where on that row
void	SetTileData(int x, int y, char data)	{tile[ ((RESX/GRIDSIZE) * y) + x ] = data;}
And heres the vector:

std::vector <char> tile;	// holds the data for all the tiles
The vector is resized in the constructor. Anyone knows what could be wrong?
Advertisement
You're probably accessing outside the array bounds. At the bottom of the double loop you will be accessing tile[1199] (i think). Check that the array is 1200 elements long.
Make sure that you haven't left a breakpoint in your IDE.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Im indeed accessing out of bounds. When Im printing the char it first prints just zeros (which I filed the vector with by doing .resize( sizeX * sizeY, '0');) but then it starts to print all sorts of wierd numbers and characters.

Then I added a std::cout << tile.size(); and it tells me that the vector actually contains 1200 elements... should be enough...

Any more ideas?

[Fruny] You posted while I was writing, Ive made sure there are no breakpoints. For now Ive got that function commented out but there seems to be some out of bounds issue.
what code are you using to print the chars?
This function is used to render:

void Game::RenderWorld() {	std::cout << tile.size() << std::endl; // print the size of the vector to check if its big enough	for(int y = 0; y < sizeX; y++) {		for(int x = 0; x < sizeY; x++) { /* No images, we get it working first			if(GetTileData(x, y) == 0)				sdl.DrawImg("0", x*GridSize, y*GridSize);			else				sdl.DrawImg("1", x*GridSize, y*GridSize);                        */			std::cout << GetTileData(x, y) << " "; // print 		}	}}


char	GetTileData(int x, int y)   {return tile[ (sizeX * y) + x ];}
you seem to have your x's and y's the wrong way round.

you have
for ( int y = 0; y < sizeX; y++ )
instead of
for ( int y = 0; y < sizeY; y++ )

this leave you accessing something like 1500 or so elements instead of 1200

its also like this in the original post - just didn't spot it!
Hehe yeah I did just spot it myself :P How stupid... Well thanks for the help! :D

This topic is closed to new replies.

Advertisement