Screen closes on mouse motion

Started by
2 comments, last by Penanito 10 years, 2 months ago

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?

Advertisement

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?)

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)?

Yeah turned it into a regular variable and everything worked fine :) thanks

This topic is closed to new replies.

Advertisement