Sign in to follow this  
willthiswork89

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

Recommended Posts

willthiswork89    100
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]

Share this post


Link to post
Share on other sites
Oberon_Command    6088
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.

Share this post


Link to post
Share on other sites
Nairb    766
Show us the code. That error could be happening for any number of reasons. You're most likely accessing invalid memory somewhere.

Cheers,
--Brian

Share this post


Link to post
Share on other sites
willthiswork89    100

//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 32

SDL_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();
}




Share this post


Link to post
Share on other sites
Will F    1069
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.

Share this post


Link to post
Share on other sites
rip-off    10979
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.



Share this post


Link to post
Share on other sites
willthiswork89    100
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

Share this post


Link to post
Share on other sites
nullsquared    126
Quote:
Original post by willthiswork89
Can anyone tell me what this does?

filename.c_str()

i dont understand what c_str is?


A C++ string is an object, which, IIRC is a dynamic char array (don't quote me on this [grin]). It has member functions. For example, like c_str(). c_str in a string instance would return the C-style string, or (I think it's a const) a char array. So simple you're modifying it with a normal C++ string, but you can use it like a C - style string with c_str().

Anyways, yes, do check that your surfaces aren't null.

@rip-off:
I just looked at his code... He really does that? Maybe he edited his post...

Share this post


Link to post
Share on other sites
willthiswork89    100
i put it all into one and now the screen if just black the image wont show up...i dont get wtf is going on....

heres my code if anyone can help out...sorry im really new to sdl lol


#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <string>
using namespace std;
#define SWIDTH 640
#define SHEIGHT 480
#define BPP 32

SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;

SDL_Event event;

SDL_Surface *load_image(string filename)
{
SDL_Surface *temp = NULL;
SDL_Surface *final = NULL;

temp = SDL_LoadBMP(filename.c_str());

if(temp != NULL)
{
final = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
}
else{ SDL_Quit(); }
return final;
}

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()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
screen = SDL_SetVideoMode(SWIDTH,SHEIGHT,BPP,SDL_SWSURFACE);

if(screen == NULL)
{
return false;
}

SDL_WM_SetCaption("Shh Event Driving",NULL);

return true;
}

bool load_files()
{
image = load_image("hello_world.bmp");

if(image == NULL)
{
return false;
}
return true;
}

void clean_up()
{
SDL_FreeSurface(image);
SDL_Quit();
}

int main(int argc, char* args[])
{
bool quit = false;

if(init() == false)
{
return 1;
}

if(load_files == false)
{
return 1;
}

apply_surface(0,0,image,screen);

if(SDL_Flip(screen) == -1)
{
return 1;
}

while(!quit)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
quit = true;
}
}

clean_up();

return 0;
}



everything compiles.. AND runs just no actual images being shown

Share this post


Link to post
Share on other sites
rip-off    10979

int main(int argc, char* args[])
{
bool quit = false;

if(init() == false)
{
return 1;
}

/*
i added the "()" here after the identifier load_images
you are comparing a functions address with false
its odd yes but it compiles and will run

*/

if(load_files() == false)
{
return 1;
}

apply_surface(0,0,image,screen);

if(SDL_Flip(screen) == -1)
{
return 1;
}

while(!quit)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
quit = true;
}
}

clean_up();

return 0;
}




note the comment above load files call

Share this post


Link to post
Share on other sites

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