Sign in to follow this  
Penanito

Screen closes on mouse motion

Recommended Posts

Penanito    381

Basically whenever i run the code, everything will work fine, but as soon as the cursor goes inside the SDL window, i get an error :Unhandled exception at 0x011af169 in game10.exe: 0xC0000005: Access violation writing location 0xcdcdcdcd. (visual studio 2010)

while(SDL_PollEvent(&mainEvent))
	{
		if(mainEvent.type == SDL_QUIT)
		{
			TheGame::Instance()->quit();
		}
	if(mainEvent.type == SDL_MOUSEMOTION)
	{
		m_mousePosition->setX(mainEvent.motion.x);
		m_mousePosition->setY(mainEvent.motion.y);
	}

Basically whenever theres mouse motion it should call the function SetX through the m_mousePosition object right?

(part of m_mousePosition's class):

class Vector2D
{
public:
    Vector2D()
    {
        m_x = 0;
        m_y = 0;
    }
    Vector2D(float x, float y) : m_x(x), m_y(y) {}

    float getX() { return m_x; }
    float getY() { return m_y; }

    void setX(float x) { m_x = x; }//function being called, also the next statement to be executed when break happens
    void setY(float y) { m_y = y; }//function being called

private:

    float m_x;
    float m_y;
};

Whenever i comment out the SetX and SetY functions, the problem stops, so i only assumed this is where the problem happened, any ideas on what could be going wrong?

Share this post


Link to post
Share on other sites
DejaimeNeto    4221

It is a common problem with bad memory accesses.

You could test changing the lines to this:

m_mousePosition->setX( 1 );
m_mousePosition->setY( 1 );

and see what happens.

 

A good thing would also be using the Debugger, to see the callstack.

 

One question: are you running your application from a terminal (console)?

 

About m_mousePosition, as you're using -> it is a pointer, right?

Have you allocated the memory for that pointer? (used new or anything to initialize it?)

Edited by dejaime

Share this post


Link to post
Share on other sites

As @dejaime said, 'm_mousePosition' is almost certainly a bad pointer, especially since the memory address (0xcdcdcdcd) is Visual Studio magic number that means 'uninitialized heap memory' (when compiled in debug mode).

 

Is m_mousePosition actually being pointed at another variable or some dynamically allocated memory?

Also, why is m_mousePosition a pointer and not a regular variable (it should most likely be a regular variable, or else a reference)?

Edited by Servant of the Lord

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