Jump to content

  • Log In with Google      Sign In   
  • Create Account

Screen closes on mouse motion


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Penanito   Members   -  Reputation: 284

Like
0Likes
Like

Posted 05 February 2014 - 07:17 PM

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?



Sponsor:

#2 dejaime   Crossbones+   -  Reputation: 4119

Like
0Likes
Like

Posted 05 February 2014 - 07:41 PM

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, 05 February 2014 - 07:49 PM.


#3 Servant of the Lord   Crossbones+   -  Reputation: 21206

Like
0Likes
Like

Posted 05 February 2014 - 08:07 PM

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, 05 February 2014 - 08:08 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 Penanito   Members   -  Reputation: 284

Like
0Likes
Like

Posted 05 February 2014 - 08:31 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS