Access violation reading...

Pages: 12
Hello, this is for my survival game again.

Im trying to make it so that when right is pressed the player will move right and it will loop through his running animation frames.
Im getting this error:
1
2
3
4
Unhandled exception a 0x00215469 in MyGame.exe: 0xC00000005: Access violation 
reading location 0xcdcdce45.

[Break][Continue]


Here is the relevant code:

In Input.cpp
1
2
3
4
5
6

// KeysHeld[] is an array of booleans
bool Input::KeyHeld(int key)
{
	return KeysHeld[key];
}


in player.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void Player::HandleEvents( SDL_Event event )
{
//Hanlde key down event
	if( input->KeyHeld( SDLK_d ) )
	{
		direction = RIGHT;
		frame++;

		xVel += 10;
	}

        ...
 // Handle Key up event
	if( input->KeyHeld( SDLK_d ) == false )
	{
		frame = 0;

		if( xVel = -10 )
			xVel += 10;
	}
        
        ...

 //loop the animation
	if( frame >= 3 )
	{
		frame = 0;
	}
closed account (zb0S216C)
When you use key as the index within KeyHeld( ), you never validate[1] the value held by key. Since you don't validate it, you could be accessing memory that isn't yours.

References:
[1] In this context, check if the value is between 0 and X.


Wazzak
What line of code is the access violation occuring on?

Is it this one? return KeysHeld[key];

If yes, it looks like the 'this' pointer is bad, which means whatever object you're using to call the KeyHeld function is bad

IE:
1
2
input->KeyHeld( foo ); // <- a bad 'input' here would
    // case 'this' to be bad in KeyHeld 
What do you mean a bad 'input'?

was i suppose to do this..

Input* input = new Input();
...
delete input;

If so woops lol
Not necessarily. You just need to have some kind of input object.

How is input defined / initialized?
contructor
1
2
3
4
5
6
7
Input::Input(void)
{
	for (int i=0; i<323; i++)
	{
		KeysHeld[i] = false;
	}
}


How it is intialized in player.h
 
Input* input;
OK.

Input* input;

This just gives you a pointer. It does not give it an object.

Right now, since the pointer doesn't point to anything, it's a "bad" pointer. When you try to dereference it (ie, with input->KeyHeld), you're trying to access a non-existant object.

You have to either:

1) make it point to an object
1
2
// somewhere in player.cpp when your Player is initialized
input = &some_existing_Input_object;


or
2) Get rid of the pointer and use an object:
 
Input input;  // don't use a pointer 
Okay im not getting that break error anymore, but now the character does move at all.

The code for making it move is in the OP, i dont see whats wrong with it though.
Where are you changing 'KeysHeld'?

You're probably modifying a separate Input object.
Last edited on
Im not sure what you mean by that disch.

modifying a seperate input object?
A class is like a blueprint for a "thing".

An object is the actual "thing".

Say you create a class called Apple. This tells you what an apple is, but isn't an actual apple itself. To actually create an apple, you need objects:

1
2
Apple a;
Apple b;


now we have 2 apples, a and b. a and b are "objects".

Changes made to one object do not impact other objects. Each object exists independently.

So if I do this:
 
a.TakeABite();


my a Apple will have a bite taken out of it. However the b Apple will remain unchanged (no bite).


I suspect you have 2 or more Input objects. One is in your Player class ("a") and one is somewhere else -- maybe in whatever class handles events ("b"). I suspect you are making changes to "b" and then checking for those changes in "a", which won't work because "a" is a separate object from "b".
Last edited on
Nah i only have one input object
Then where are you updating KeysHeld?
Im... im not really sure.

I need the game to check each frame if a button is being held down. I cant use the regulare SDL_PollEvent because that is just one event for down, and one event on release.

Could you please guide me in the right direction with some psuedocode?
iirc you don't need to use SDL events for this. SDL has functions for you to get the realtime status of keyboard keys. You can use those instead of having this Input class.

I don't recall the name of the functions, but I'm sure they're in SDL docs. Skim the docs to find them and try using them instead.
Okay disch i looked it up and here what i have. It still doesnt work :(.

in player.cpp HandlEvents()
1
2
3
4
5
6
7
8
9
10
Uint8* keystate = SDL_GetKeyState( NULL );

	// Handle key down events
			if( keystate[SDLK_d] )
			{
				direction = RIGHT;
				frame++;

				xVel += 10;
			}
That looks right to me.

Have you confirmed that the xVel += 10; line is being run?
How would i confirm this?
Oh, only the "direction = RIGHT;" line is running.

thats strange.
okay my code looks like the code below now. The player moves left and right fine, but is not looping through the animation frames.

EDIT: weird stuff is happening. If a directional button and another button are pressed he goes real fast and breaks the scrolling mechanism. And sometimes jumping works, and sometimes is doesnt.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Uint8* keystate = SDL_GetKeyState( NULL );

	// Handle key down events
			if( keystate[SDLK_d] )
			{
				direction = RIGHT;
				frame++;

				xVel += 10;
			}
			else if( keystate[SDLK_a] )
			{
				direction = LEFT;
				frame++;

				xVel -= 10;
			}
			else
			{
				frame = 0;
				xVel = 0;
			}
Last edited on
Pages: 12