Sign in to follow this  
Mizipzor

"Unhandled exception"

Recommended Posts

Mizipzor    247
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?

Share this post


Link to post
Share on other sites
JohnHurt    334
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.

Share this post


Link to post
Share on other sites
Mizipzor    247
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.

Share this post


Link to post
Share on other sites
Mizipzor    247
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 ];}

Share this post


Link to post
Share on other sites
JohnHurt    334
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this