# Access violation (segment fault)

This topic is 4099 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Yep, that's the error i get when i run my SDL program - "An Access Violation (Segmentation Fault) raised in your program." I've ran into one of these before and i think i know that it's something to do with it trying to access memory which isn't there. I know that the problem is somewhere in this code...
void Character::ApplyAnimCharacter( vector<int> anim, int speed )
{
AnimationSpeed = speed;
if( PausedTime == AnimationSpeed )
{
frame++;
if( frame > anim.size() )
{
frame = 0;
}
PausedTime = 0;
}
int reclip = anim[frame];
apply_surface( position.x - camera.x, position.y - camera.y, SpriteSheet, screen, &clips[reclip] );
PausedTime++;
}


In the main file:
//this is the animation vector
vector<int> DownWalk( 8 );
DownWalk[0] = 0;
DownWalk[1] = 1;
DownWalk[2] = 2;
DownWalk[3] = 1;
DownWalk[4] = 0;
DownWalk[5] = 3;
DownWalk[6] = 4;
DownWalk[7] = 3;
//this is how it's used:
//ringo is a character class
Ringo.ApplyAnimCharacter( DownWalk, 10 );


As you can probly see i'm making an animated character appear based on the main game loop... But i was wondering if anyone could help me see the problem? Thanks for replies...

##### Share on other sites
First, a suggestion: use "const vector<int>&" for your function parameter. That will pass a reference to the vector, rather than copying the vector itself. Since a reference is under normal circumstances the size of a pointer (4 or 8 bytes usually, depending on the platform), and the contents of a vector can get pretty large, it is far easier to pass a reference, and could potentially save you a whole lot of performance time. In any case, it is one of those "micro-optimizations" that one should rarely hesitate to make the first time around.

Secondly, and regarding the actual problem, what do the variables PausedTime and frame get initialized to? If PauseTime does not equal AnimationSpeed, and if frame is some invalid value, then you'll end up trying to access anim[frame], which will likely lead to the access violation. (Note that by using a debugger, you could find this problem very easily. Learn to use your debugger, and you will be very very thankful for its existence. Debuggers are beautiful.)

Another possibility is that clips[reclip] is accessing an invalid element. But since you didn't show any code regarding those items, it's hard to say.

##### Share on other sites
In addition to what Agony says, stop on this line in your debugger

apply_surface( position.x - camera.x, position.y - camera.y, SpriteSheet, screen, &clips[reclip] );

Pay attention to the value of SpriteSheet, screen and clips going in.

The cause of the bug isnt directly in the code you pasted.

##### Share on other sites
In this code:

void Character::ApplyAnimCharacter( vector<int> anim, int speed ){     AnimationSpeed = speed;     if( PausedTime == AnimationSpeed )     {         frame++;         if( frame > anim.size() )         {             frame = 0;         }         PausedTime = 0;     }     int reclip = anim[frame];     apply_surface( position.x - camera.x, position.y - camera.y, SpriteSheet, screen, &clips[reclip] );     PausedTime++;}

I think the line

if ( frame > anim.size() )

should be changed to

if ( frame >= anim.size() )

That way, if your animation has, for example, 8 frames, on frame #8 (which is out of bounds, since the animation vector has 8 elements numbered 0-7) it will go back to frame 0. The way you're doing it, it goes through 9 elements (0-8) instead of 8 (0-7). That looks like the problem, unless the issue is somewhere in apply_surface instead.

##### Share on other sites
Well i'll be daamned...

After a bit of fiddling i changed:
if( PausedTime == AnimationSpeed )
to:
if( PausedTime >= AnimationSpeed )
I'm not sure why that worked but i guess i'll look into it more.
Thanks a lot.

##### Share on other sites

This topic is 4099 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.