Jump to content
  • Advertisement
Sign in to follow this  
skjinedmjeet

SDL Mouse input causing fatal crashes

This topic is 4840 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

I've been trying to track down the most bizarre bug I've ever run into. Basically, my program uses OpenGL and SDL, and I have keyboard input currently implemented. In adding mouse input I'm experiencing what seem to be completey random fatal crashes. I've disabled the hardware cursor - which I originally thought was causing problems, but crashes starting reoccuring. Sometimes I can let my program run for hours, and it's fine. Sometimes it crashes within 30 seconds, but I've ascertained that this is the code that's causing a crash.
	SDL_GetMouseState(&mx,&my);
//	mx=rand()%640;                   // for debug purposes
//	my=rand()%480;	
	if(mx<0)mx=0;if(mx>640) mx=640;
	if(my<0)my=0;if(my>480) my=480;
Now, if I replace that with the following, commenting SDL_GetMouseState and uncommenting the mx=rand()%640 lines:
//	SDL_GetMouseState(&mx,&my);
	mx=rand()%640;                   // for debug purposes
	my=rand()%480;	
	if(mx<0)mx=0;if(mx>640) mx=640;
	if(my<0)my=0;if(my>480) my=480;
My program will run overnight without crashing. When it does decide to crash, Dev-C++'s debugger shows the program as exiting normally. If I don't move the mouse, the program will run overnight just fine. If I do wave the mouse around, it will often crash, but not always. Anybody got any ideas, or have experienced this before?

Share this post


Link to post
Share on other sites
Advertisement
Few things:

1 - 'mx' and 'my' are declared as int, correct?
2 - Set a breakpoint after SDL_GetMouseState(&mx,&my); and see what the reported values of mx and my are. If they are garbage, then you have a buffer overflow before this code.
3 - Try adding a SDL_PumpEvents(); before the SDL_GetMouseState call
4 - The problem lies somewhere else that is affecting that call, so more code [wink]

Share this post


Link to post
Share on other sites
mx and my are both int.


SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER );
SDL_WM_GrabInput( SDL_GRAB_ON );
SDL_ShowCursor(SDL_DISABLE);
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER , 1 );
SDL_SetVideoMode(640, 480, 0, SDL_OPENGL | SDL_FULLSCREEN | SDL_INIT_NOPARACHUTE );


Setting a breakpoint could be tricky, as it will run through the main loop thousands of times before crashing. I'll log the values of mx and my in a file though, although I'm not sure if that will catch it.

edit: SDL_PumpEvents() Hasn't solved it. [sad]

And for completeness, logging the values hasn't given me any good hints as to what's going on. I'd be tempted to just disregard mouse input and use the working keyboard input, except that I think this might be symptomatic of another problem.

I can only duplicate the crash on two of my three machines.

Share this post


Link to post
Share on other sites
If you want you can try to do a plug-in-play with this old input library and see if the mouse functionality crashes as well. If it still does, then you might need to reinstall SDL and check drivers. If not, you can start revising.

Share this post


Link to post
Share on other sites
Yeah, I wrote a new main() just to see if it was anything I'd written that was causing a crash. Having just installed SDL though, I think everything should be okay. Argh. Anyway, your input library works great, and doesnt cause a crash. I'd like to be able to use the SDL commands. I also thought it may be a driver issue, but it happens on multiple computers.

I think the problem is exacerbated by using all GNU tools.


int main(int argc, char *argv[]){

int mx, my;
//GLuint Light; //commented out for debugging
SDL_Event event;
ofstream Log("Log.txt");


SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER );
SDL_WM_GrabInput( SDL_GRAB_ON );
SDL_ShowCursor(SDL_DISABLE);


SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER , 1 );
SDL_SetVideoMode(640, 480, 0, SDL_OPENGL | SDL_FULLSCREEN | SDL_INIT_NOPARACHUTE );

//Light=loadtexture("Data\\LightSource.png"); //commented, for debugging


glViewport(0, 0, 640, 480);
glClearColor(0.0f,0.0f, 0.0f, 0.0f);
glClearDepth(1.0);


glShadeModel(GL_SMOOTH);


glMatrixMode(GL_PROJECTION);
glOrtho(0.0f,640,480,0,-1.0f,1.0f);

glMatrixMode(GL_MODELVIEW);
int done;


for(done = 0; !done;){

SDL_PumpEvents();
SDL_GetMouseState(&mx,&my); //commenting this line prevents crash

Log<<mx<<"\n";
Log<<my<<"\n";

if(mx<0)mx=0;if(mx>640) mx=640;
if(my<0)my=0;if(my>480) my=480;

//drawsprite(Light,float(mx),float(my) , 128); //commented, for debugging

SDL_GL_SwapBuffers();

SDL_PollEvent(&event);
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
}

SDL_Quit();
return(0);
}


Anyway, huge thanks for taking the time to respond.

Share this post


Link to post
Share on other sites
Oh well with that code, you don't have a message processing loop! Try something like this:


int main(int argc, char *argv[]){

int mx, my;
//GLuint Light; //commented out for debugging
SDL_Event event;
ofstream Log("Log.txt");


SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER );
SDL_WM_GrabInput( SDL_GRAB_ON );
SDL_ShowCursor(SDL_DISABLE);


SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER , 1 );
SDL_SetVideoMode(640, 480, 0, SDL_OPENGL | SDL_FULLSCREEN | SDL_INIT_NOPARACHUTE );

//Light=loadtexture("Data\\LightSource.png"); //commented, for debugging


glViewport(0, 0, 640, 480);
glClearColor(0.0f,0.0f, 0.0f, 0.0f);
glClearDepth(1.0);


glShadeModel(GL_SMOOTH);


glMatrixMode(GL_PROJECTION);
glOrtho(0.0f,640,480,0,-1.0f,1.0f);

glMatrixMode(GL_MODELVIEW);
int done;

// while > for in this case ;)
while( !done )
{
while ( SDL_PollEvent(&event) )
{
// you should handle the quit message here and nething else
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
}

SDL_GetMouseState(&mx,&my); //commenting this line prevents crash

Log<<mx<<"\n";
Log<<my<<"\n";

if(mx<0)mx=0;if(mx>640) mx=640;
if(my<0)my=0;if(my>480) my=480;

//drawsprite(Light,float(mx),float(my) , 128); //commented, for debugging

SDL_GL_SwapBuffers();

}

SDL_Quit();
return(0);
}


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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!