Help! SDL program crashing on me***Fixed***

Started by
14 comments, last by willthiswork89 18 years, 4 months ago
i coded a sdl program and its crashing, i opened the stderr.txt and the problem states this Fatal signal: Segmentation Fault (SDL Parachute Deployed) if you need to see my code i will show you its a fairly small program just to load files... [Edited by - willthiswork89 on November 26, 2005 3:24:43 PM]
Advertisement
The files that you are trying to load might not exist. Or, rather, you may be giving SDL the wrong paths. Check to make sure that your paths are correct.

If you still get the error, try running the code in a debugger or start commenting lines out to find out what line is crashing the program. Once you have that, post it here.
im using bloodshed dev and ive never used a "debugger" im a 7 month programmer so intermediate but i wouldlove to know how to debug
Show us the code. That error could be happening for any number of reasons. You're most likely accessing invalid memory somewhere.

Cheers,
--Brian
//main.h#include "applyimage.h"#include <SDL/SDL.h>#include <fstream>SDL_Surface *image = NULL;SDL_Surface *screen = NULL;SDL_Event event;int main(int argc, char* args[]){ bool quit = false;  if(init(screen) == false) {                 return 1;}if(load_files(image)  == false){                      return 1;}apply_surface(0,0,image,screen);if(SDL_Flip(screen) == -1){                    return 1;}while(quit == false){while(SDL_PollEvent(&event)){if(event.type == SDL_QUIT ){quit = true;}}}cleanup(image);return 0;}


//applyimage.h#include<SDL/SDL.h>#include<string>using namespace std;#define SWIDTH 640#define SHEIGHT 480#define BPP 32SDL_Surface *load_image(string filename);void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination);bool init(SDL_Surface *surface);bool load_files(SDL_Surface *surface);void cleanup(SDL_Surface *surface);

//applyimage.cpp#include <SDL/SDL.h>#include <SDL/SDL_image.h>#include <string>#include "applyimage.h"using namespace std;SDL_Surface *load_image(string filename){            SDL_Surface *loadedimage = NULL;            SDL_Surface *changedbpp = NULL;                                    loadedimage = SDL_LoadBMP(filename.c_str());                                    if(loadedimage != NULL)            {            changedbpp = SDL_DisplayFormat(loadedimage);                        SDL_FreeSurface(loadedimage);            }return changedbpp;}void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination){     SDL_Rect ofs;          ofs.x = x;     ofs.y = y;          SDL_BlitSurface(source,NULL,destination,&ofs);}bool init(SDL_Surface *surface){      if(SDL_Init(SDL_INIT_EVERYTHING) == -1)    {    return false;    }surface = SDL_SetVideoMode(SWIDTH,SHEIGHT,BPP,SDL_SWSURFACE);if(surface == NULL){                      return false;        }SDL_WM_SetCaption("Shh...Event Driving!", NULL);return true;}bool load_files(SDL_Surface *surface){     surface = load_image("background.bmp");     if(surface == NULL)     {             return false;                          }              return true;}void cleanup(SDL_Surface *surface){     SDL_FreeSurface(surface);     SDL_Quit();}            
Try adding the following lines to your code:

// in SDL_Surface *load_image(string filename)if (NULL == loadedimage)    cout << "Could not load: " << filename << endl;


From a quick look at your code, it looks like if loading an image fails you return a surface pointing to NULL. And then later try to blit that surface. The above won't fix the problem, but might give you an idea of what is not working.

Also think about adding a check to apply_surface() to make sure *source and *destination don't point to NULL. As i'm about 99% sure the problem is that you're trying to blit a NULL surface.
your functions

bool init(SDL_Surface *surface) and bool load_files(SDL_Surface *surface)

wont act like you expect them to.

they will *not* modify the values of screen and image that are in main().

this is because the variables are copied.
you are creating a new SDL_Surface pointer,pointing it at the screen, then discarding it.

as a solution you need to return some value from these functions.

so load_files should return a SDL_Surface* and can be used like so

SDL_Surface *load_file(){   return SDL_LoadBMP( "background.bmp" );}


you can then check if the return value was NULL for error checking.



i dont understand why it wouldnt beable to access the bmp because its right there and before these file splitting midifications it worked so i dunno thats hwy i dont think opening ht eimage is a problem but i will try
ohhh okay see im new to file splitting let me try that
then the file splitting cant work for this...thats where im always getting confused because its making copies...it all has to be in one file like everything else i try.. grr

This topic is closed to new replies.

Advertisement