Advertisement Jump to content
Sign in to follow this  
Penanito

Screen closes on mouse motion

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!