• Advertisement
Sign in to follow this  

Strange SDL error: Segmentation Fault

This topic is 4612 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 working with SDL since nearly 3 days and I tried to move an image today. When executing the code in VC++6, everything works fine. But when I execute the *.exe by myself (by double clicking on it ;)) SDL throws the following error: Fatal signal: Segmentation Fault (SDL Parachute Deployed) When starting the exe out of the Visual Studio, it works fine. Here is the code:
#include <stdlib.h>
#include <SDL.h>

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

	bool isRunning = true;

	int sdlInit;

	int imageX = 0;
	int imageY = 0;

	Uint8 *keys;

	SDL_Surface *screen,*image,*temp;
	SDL_Event event;
	SDL_Rect rect;

	sdlInit = SDL_Init(SDL_INIT_VIDEO);

	if(sdlInit == -1) {

		printf("Couldn't init SDL: %s\n",SDL_GetError());
		exit(1);

	}

	atexit(SDL_Quit);

	screen = SDL_SetVideoMode(640,480,16,SDL_HWSURFACE | SDL_DOUBLEBUF);

	if(screen == NULL) {

		printf("Couldn't set video mode: %s\n",SDL_GetError());

	}

	SDL_WM_SetCaption("SDL Moving Images",NULL);

	temp = SDL_LoadBMP("airplane.bmp");

	image = SDL_DisplayFormat(temp);

	SDL_FreeSurface(temp);

	// SDL_SetColorKey(image,SDL_SRCCOLORKEY,SDL_MapRGB(image->format,255,255,255));

	while(isRunning) {

		if(SDL_PollEvent(&event)) {

			switch(event.type) {

			case SDL_QUIT:

				isRunning = false;

				break;

			}

		}

		keys = SDL_GetKeyState(NULL);

		if(keys[SDLK_UP]) {

			if(imageY > 0) {

				imageY--;

			}

		}

		if(keys[SDLK_DOWN]) {

			if(imageY < 480-image->h) {

				imageY++;

			}

		}

		if(keys[SDLK_LEFT]) {

			if(imageX > 0) {

				imageX--;

			}

		}

		if(keys[SDLK_RIGHT]) {

			if(imageX < 640-image->w) {

				imageX++;

			}

		}

		if(keys[SDLK_ESCAPE]) {

			isRunning = false;

		}

		rect.x = imageX;
		rect.y = imageY;

		SDL_BlitSurface(image,0,screen,&rect);

		SDL_Flip(screen);

		SDL_FillRect(screen,0,SDL_MapRGB(image->format,0,0,0));

	}


	return 0;

}
SDLmain.lib and SDL.lib are linked and I activated multithreaded DLL. What's going on there? Thanks for help, Christian.

Share this post


Link to post
Share on other sites
Advertisement
Quote:

temp = SDL_LoadBMP("airplane.bmp");


That's the problem right there. You aren't checking temp, if SDL_LoadBMP fails, it'll return NULL, and SDL_DisplayFormat will explode when you give it a null pointer.

This happened because when you runa project in visual studio, it starts out in the PROJECT directory, but the EXE is in PROJECT/Debug or PROJECT/Release.

If you just double click the EXE, it'll start in PROJECT/Debug or PROJECT/Release, so it won't find the BMP files (which must be in PROJECT if they are loaded correctly when you hit run)
Try moving the EXE into the same directory as the BMPs, and running it from there.

Share this post


Link to post
Share on other sites
To handle the error correctly, try this:



temp = SDL_LoadBMP("airplane.bmp"); // Loading airplane.bmp

if(temp==NULL) // If airplane.bmp isn't found
{

atexit(SDL_Quit); // Stops SDL when the program exits

return 0; // Exit the program

}






EDIT: You're already doing that, at least for "screen" (your main Game Window)?

Same principle applies.

HTH,

ukdeveloper.

Share this post


Link to post
Share on other sites
Hi,

thanks for your fast replies. Yes, screen is the main game window. Usually I check all loadings, but I thought it would run without checking in this small example. Thanks again.

Christian

Share this post


Link to post
Share on other sites
Segmentation Fault errors usually happen when you're making reference to a pointer that wasn't initialized or isn't there anymore. Start up your debugging skill and find out where it is. Good luck!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement