Crashes When Character Walks

Started by
11 comments, last by LeftyGuitar 9 years, 8 months ago

Hello,

Whenever I try to move my character using the keyboard, it crashes after a few second. I'll post the code and where it says the crash is coming from.


gPlayer.XPos = MAX_WIDTH / 2;
	gPlayer.YPos = MAX_HEIGHT / 2;
	gPlayer.XVel = 0.010f;
	gPlayer.YVel = 0.010f;
	gPlayer.CurrentFrame = 1;

for(int i = 0; i <= 8; i++)
	{
		gPlayer.LeftRect[i].x = gPlayer.XPos;
		gPlayer.LeftRect[i].y = gPlayer.YPos;
		gPlayer.LeftRect[i].w = gPlayer.Width;
		gPlayer.LeftRect[i].h = gPlayer.Height;
	}

else if(event.key.keysym.sym == SDLK_LEFT)
				{
					gPlayer.Facing = gPlayer.LEFT;
					gPlayer.CurrentState = gPlayer.WALK_LEFT;
				}

else if(event.key.keysym.sym == SDLK_LEFT)
				{
					gPlayer.CurrentState = gPlayer.IDLE;
					gPlayer.Facing = gPlayer.LEFT;
					gPlayer.XVel = 0.0f;
				}

if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
	{
		gPlayer.LeftRect[gPlayer.CurrentFrame].x -= 3;
	}

if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
	{
		SDL_RenderCopy(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame]);

		gPlayer.CurrentFrame++; //This is where the debugger points to when it crashes
		gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;
	}

I've only posted bits and pieces of my code. I can post more if it is needed. My problem is it runs fine when first executed, but when I try to move the character, it shows the animation play, then crashes after a couple of seconds. NOTE: There are 8 animations for the character walking.

Advertisement
How big is your leftrect array?

<= 8 looks suspicious
Also seems likely you AR accessing your arrays out of bounds with current frame too
Required reading.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

The link on the what the debugger numbers mean is down. Also, The LeftRect array is 8. I tried changing it to 9, but still nothing. Here is what the debugger error message says

First-chance exception at 0x6C778E1B (SDL2.dll) in KnightsReverie.exe: 0xC0000005: Access violation reading location 0x00000200.
Unhandled exception at 0x6C778E1B (SDL2.dll) in KnightsReverie.exe: 0xC0000005: Access violation reading location 0x00000200.

I tried looking those up, but I couldn't really find anything.


	for(int i = 0; i == 8; i++)
	{
		gPlayer.LeftRect[i].x = gPlayer.XPos;
		gPlayer.LeftRect[i].y = gPlayer.YPos;
		gPlayer.LeftRect[i].w = gPlayer.Width;
		gPlayer.LeftRect[i].h = gPlayer.Height;
	}

I also tried this, and still nothing. The idle animations play fine, but when I try to move the character with walking animations, it crashes.

What's the value of gPlayer.CurrentFrame when it crashes? Go over the values of all your variables in the debugger when it bombs and you should get a hint.

I tried setting a watch on the variable, but it says unrecognized token for gPlayer.CurrentFrame. I also noticed these two errors SDL2.dll!6c778e1b() Unknown and [Frames below may be incorrect and/or missing, no symbols loaded for SDL2.dll]

I also added this to code, but it dosen't seem to help.


	if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
	{
		SDL_RenderCopy(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame]);

		gPlayer.CurrentFrame++;
		gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;

		if(gPlayer.CurrentFrame >= 8)
		{
			gPlayer.CurrentFrame = 1;
		}
	}

I think I am getting closer to fixing the problem.

This crash is inside SDL, not your code. You're probably misreading the debugger's indicator (it shows the next line that will execute, not the line that caused the crash). I would verify that you're not passing something bogus to SDL_RenderCopy.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]


	if(gPlayer.Facing == gPlayer.RIGHT)
	{
		SDL_RenderCopyEx(GameRend,gPlayer.IdleSideTex[gPlayer.CurrentFrame],NULL,&gPlayer.IdleSideRect[gPlayer.CurrentFrame],NULL,NULL,SDL_FLIP_HORIZONTAL); //Could it be something to do with this?

		gPlayer.CurrentFrame++;
		gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 9;
	}

	if(gPlayer.CurrentState == gPlayer.WALK_LEFT)
	{
		SDL_RenderCopy(GameRend,gPlayer.LeftTex[gPlayer.CurrentFrame],NULL,&gPlayer.LeftRect[gPlayer.CurrentFrame]); //Or this?

		gPlayer.CurrentFrame++;
		gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;

		if(gPlayer.CurrentFrame >= 8)
		{
			gPlayer.CurrentFrame = 1;
		}
	}

I looked over my SDL_RenderCopy(s), and I didn't notice anything bogus. Everything inside those calls has values, which can and are being used. One is RenderCopyEx, to flip the image when needed. Should I make them all RenderCopyEx(s)? I doubled check, and the images for walking is only 8 frames. However for standing idle, left or right, there is 9 images.

Print the value of gPlayer.CurrentFrame (right before each RenderCopy call) so you can see in a log/output window what the last value is.

You could also hover the variable when the application crashes, to see its value. This might be IDE specific, but the ones I know about support this.

You're only posting snippets, which makes it impossible to guess the overall flow of the thing, but if the last code you posted is directly copied, I think i see the reason for it, plus some other issues:


gPlayer.CurrentFrame++;
gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8; //Also in the facing code, with the % 9

You first increment the frame by one, but this is a complete waste. The very next line you set the variable directly, overwriting the increment operation.


gPlayer.CurrentFrame = SDL_GetTicks() / 150 % 8;
if (gPlayer.CurrentFrame >= 8)
// ...

If I'm reading this correctly (I can't recall the operator precedence rules here), this if will never trigger.

I'm reading it as equivalent to (SDL_GetTicks() / 150) % 8, which means that the result will always be between (and including) 0 and 7. Your if check checks for 8 and higher, which is never true.


if (gPlayer.Facing == gPlayer.RIGHT)
//stuff
if (gPlayer.CurrentState == gPlayer.WALK_LEFT)
//Stuff that crashes

I'm assuming you also have a facing == gPlayer.LEFT somewhere. If you actually execute the facing (idle?) drawing always, and the overwrite with the walking animation, this will result in out of bounds reading.

The % 9 in the facing/idle code will make gPlayer.CurrentFrame be between (and including) 0 and 8. If it's set to 8, and then the walking animation code is run directly after, you will be indexing into your 8 large array (with max valid index = 7) with the invalid index

--> Crash, access violation.

Hello to all my stalkers.

This topic is closed to new replies.

Advertisement