Sign in to follow this  

Segmentation fault on button press

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

Hi, I'm just trying to get an event handler running with SDL. When pressing a button while running this code I get Segmentation fault. Any idea why?
#include "SDL/SDL.h"
#include <stdio.h>

int main(int argc, char *argv[])
{
	/* Initialize stuff */
	SDL_Surface *screen;
	SDL_Event event;
	
	int quit = 0;
	
	atexit(SDL_Quit);
	
	if (SDL_Init(SDL_INIT_VIDEO) > 0)
	{
		fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
		exit(1);
	}
	
	screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
	
	if (screen == NULL)
	{
		fprintf(stderr, "Could not set video mode: %s\n", SDL_GetError());
		exit(1);
	}
	
	/* The game loop */
	while (!quit)
	{
		/* Check input */
		while (SDL_PollEvent(&event))
		{
			switch (event.type) 
			{
				case SDL_KEYDOWN:
					printf("The %s key was pressed!\n", event.key.keysym.sym);
					quit = 1;
					break;
				case SDL_KEYUP:
					printf("The %s key was released!\n", event.key.keysym.sym);
					quit = 1;
					break;
				default:
					printf("Press any key on the keyboard.\n");
					break;
			}
		}
		printf("Event queue empty.\n");
		
		/* Play ball! */
	}
	
	exit(0);
}

Share this post


Link to post
Share on other sites
It may be because you're passing an integer to printf() where a string is expected. (While we're on the topic, any particular reason you're coding in C rather than C++?).

Share this post


Link to post
Share on other sites
Your format specifiers are wrong. %s is for a null-terminated char array, not a char.

BTW: Problems like this are best found with the debugger. If you don't yet know how to use it, you owe it to yourself to learn.

Share this post


Link to post
Share on other sites
Thanks, replacing with either %d or %c fixed the problem. :) I'll also look around for a debugger I could use (I'm not working in an IDE, thought it would be overkill for me). I can't say that I have any particular reason for choosing C instead of C++. It seemed simple enough, and I had just read through this http://www2.its.strath.ac.uk/courses/c/. And anyway, I know C++ is more widely used in games, but I don't really understand why.

Share this post


Link to post
Share on other sites
Quote:
Original post by fredrikhcs
Thanks, replacing with either %d or %c fixed the problem. :) I'll also look around for a debugger I could use (I'm not working in an IDE, thought it would be overkill for me). I can't say that I have any particular reason for choosing C instead of C++. It seemed simple enough, and I had just read through this http://www2.its.strath.ac.uk/courses/c/. And anyway, I know C++ is more widely used in games, but I don't really understand why.


Well, a reason for using C++ that you may find interesting is that you don't have this problem in C++. You don't need those format specifiers at all, and so it's impossible to get them wrong. [grin]

What compiler are you using by the way?

Interesting to hear you're not using an IDE, but not sure I agree with the "overkill" thing. Mostly, an IDE just makes things simpler for you. Of course, I think there's quite a bit of value in learning to program without an IDE, but you're not making life easier for yourself. :)

gdb is probably *the* commandline debugger.
On Windows, you might find windbg worth messing around with.

Or if you use an IDE, you'll get a nice graphical debugger to play with.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
Well, a reason for using C++ that you may find interesting is that you don't have this problem in C++. You don't need those format specifiers at all, and so it's impossible to get them wrong. [grin]

Right. The code would look something like this in C++:

std::cerr << "Could not initialize SDL: " << SDL_GetError() << std::endl;

Share this post


Link to post
Share on other sites
Quote:
Original post by fredrikhcs
I'm not working in an IDE, thought it would be overkill for me

Lesson learned, then?

Share this post


Link to post
Share on other sites
That's a very interesting point, Spoonbender. Of course, if it weren't for DevFred's comment you might've lured me into using C++ there. I'm compiling with GCC on my Ubuntu system. :) Trying out your suggestion, I let gdb debug the problem I had, but all I got from it was "Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7f16f50116e0 (LWP 6688)]
0x00007f16f4892060 in strlen () from /lib/libc.so.6". Seems a bit cryptic, and I don't see any mention of what line the problem occured on. :-|

I guess I should use an IDE, but I find it rather amusing doing everything on my own. :P

Share this post


Link to post
Share on other sites
Quote:
Original post by fredrikhcs
"Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7f16f50116e0 (LWP 6688)]
0x00007f16f4892060 in strlen () from /lib/libc.so.6". Seems a bit cryptic, and I don't see any mention of what line the problem occured on. :-|



Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb7b4b6c0 (LWP 7704)]
0xb7d1d25b in strlen () from /lib/tls/i686/cmov/libc.so.6
(gdb) backtrace
#0 0xb7d1d25b in strlen () from /lib/tls/i686/cmov/libc.so.6
#1 0xb7cec812 in vfprintf () from /lib/tls/i686/cmov/libc.so.6
#2 0xb7cf2363 in printf () from /lib/tls/i686/cmov/libc.so.6
#3 0x0804886f in main () at temp.cpp:37
(gdb)


If you knew how to use the debugger, maybe it wouldn't be so cryptic. For an easier debugging experience without an IDE (I don't use one either), try ddd.

Share this post


Link to post
Share on other sites
Quote:
Original post by fredrikhcs
That's a very interesting point, Spoonbender. Of course, if it weren't for DevFred's comment you might've lured me into using C++ there.

I don't see how DevFred's comment could have dissuaded you. If anything, it's an example of how simple and safe it is to produce text output in C++.

But the elegance of streams is not the major reason to use C++ over C, IMHO. Proper OO constructs such as inheritance and polymorphism, the STL and more appropriate memory management constructs are a few examples of the numerous advantages of C++ over C in game programming. I can't even imagine going back to using C constructs for keeping track of object states (and DEFINITELY not object collections!).
Quote:
I guess I should use an IDE, but I find it rather amusing doing everything on my own. :P

Maybe for trivial programs. For complex programs, not using an IDE can dramatically reduce your productivity, unless you are very used to Emacs and Make.

Share this post


Link to post
Share on other sites
I've got another newbie problem here. The font won't display and I have no idea why. :-| It finds the font and I get no errors. But the window surface is just black.

#include <stdio.h>

#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"

/* Cleans up and exits */
int shutdown(int status);

int main(int argc, char *argv[])
{
/* Variables */
SDL_Surface *screen;
SDL_Event event;
TTF_Font *font;

int x_pos = 0, x_vel = 0, y_pos = 0, y_vel = 0;
int playing = 1;

/* Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) > 0)
{
fprintf(stderr, "SDL_Init: %s\n", SDL_GetError());
exit(1);
}

/* Set video mode */
screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);

if (screen == NULL)
{
fprintf(stderr, "SDL_SetVideoMode: %s\n", SDL_GetError());
shutdown(1);
}

SDL_ShowCursor(SDL_DISABLE);

/* Initialize SDL_TTF */
if (TTF_Init() == -1)
{
fprintf(stderr, "TTF_Init: %s\n", TTF_GetError());
shutdown(1);
}

/* Initialize the font */
font = TTF_OpenFont("data/Vera.ttf", 12);

if (font == NULL)
{
fprintf(stderr, "TTF_OpenFont: %s\n", TTF_GetError());
shutdown(1);
}

SDL_Color colorFg = {0,0,255,0}; // Blue
SDL_Surface *text = TTF_RenderText_Solid(font, "Print something please! :(", colorFg);
SDL_Rect destRect = {0,0,0,0};
SDL_BlitSurface(text, NULL, screen, &destRect);

/* The game loop */
while (playing)
{
// Check input
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYUP:
switch (event.key.keysym.sym)
{

case SDLK_q:
playing = 0;
break;
default:
break;
}
default:
break;
}
}

x_pos += x_vel, y_pos += y_vel;

/* Play ball! */

}
SDL_FreeSurface(text);
TTF_CloseFont(font);

shutdown(0);
}

/* Cleans up and exits */
int shutdown(int status)
{
if (TTF_WasInit())
TTF_Quit();
SDL_Quit();

exit(status);
}

Share this post


Link to post
Share on other sites

This topic is 3458 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.

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