Page 1 of 1

[Solved] SegFault with SDL_DisplayFormat?

Posted: Mon Jun 15, 2009 7:20 pm
by XianForce
The solution to my problem was a set of parentheses. When I called my init function, I forgot pairs of parentheses after init (which I don't think should have even compiled. Since SDL was never initialized, it caused SDL_DisplayFormat to segfault.

Re: Process terminated with status -1073741819

Posted: Mon Jun 15, 2009 7:36 pm
by rolland
With an error number that large, I'd say Windows is killing the program for some reason--most likely graphics/memory issues. If you post the modifications, someone here's bound to point out the likely cause.

Re: Process terminated with status -1073741819

Posted: Mon Jun 15, 2009 7:48 pm
by XianForce
hmm... Guess I'll post my code then...

Global Variables + Functions

Code: Select all

//SDL includes for SDL, images, text, and sound
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"

//including strings, file streaming for error logging, and string streaming to display the fps
#include <string>
#include <fstream>
#include <sstream>

//Used to write errors to a text file
std::ofstream logger("Error.txt");

//event structure
SDL_Event event;

//Dimensions of the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//Surfaces to be used
SDL_Surface* screen;
SDL_Surface* background;
SDL_Surface* sprite;


void log(std::string message)
{
    //write to the text file
    logger << message << std::endl;
    //flush logger to ensure it works next time
    logger.flush();
}

void getError()
{
    log(SDL_GetError());
    log(IMG_GetError());
    log(TTF_GetError());
    log(Mix_GetError());
}

SDL_Surface* load_image(std::string filename)
{
    //Two surfaces, one to load the image, and another to be the used image
    SDL_Surface* loadedImage;
    SDL_Surface* optimizedImage;
    //Load the image
    loadedImage = IMG_Load(filename.c_str());

    //check if the image actually loaded
    if(loadedImage != NULL)
    {
        //format the image
        optimizedImage = SDL_DisplayFormat(loadedImage);

        //delete the old image
        SDL_FreeSurface(loadedImage);
    }

    //Check if it was formatted correctly
    if(optimizedImage != NULL)
    {
        //Map a colorkey... Magenta ftw?
        Uint32 colorKey = SDL_MapRGB(optimizedImage->format, 0xFF, 0, 0xFF);
        //Make magenta transparent
        SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorKey);
    }

    //return the formatted image for use
    return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
    //make a rectangle to temporarily hold offsets
    SDL_Rect offsets;

    //put the offsets into the rectangle
    offsets.x = x;
    offsets.y = y;

    //blit the surface
    SDL_BlitSurface(source, NULL, destination, &offsets);
}

bool init()
{
    //initialize all of SDL
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        return false;
    }

    //create the screen
    screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
    //check for errors during creation of screen
    if(screen == NULL)
    {
        return false;
    }

    //Set the caption for the window
    SDL_WM_SetCaption("Practice", NULL);

    //if everything went fine, return true
    return true;
}

bool load_files()
{
    //load the background
    background = load_image("back.png");
    //load the sprite
    sprite = load_image("guy.png");

    //if something went wrong in loading... return false
    if((background == NULL) || (sprite == NULL))
    {
        return false;
    }

    //otherwise return true
    return true;
}

void clean_up()
{
    //Delete surfaces
    SDL_FreeSurface(sprite);
    SDL_FreeSurface(background);

    //shutdown SDL
    SDL_Quit();
}
Main Function

Code: Select all

int main(int argc, char* args[])
{
    //create a boolean to control the loop
    bool done = false;

    //initialize everything, then log error if anything went wrong.
    if(init == false)
    {
        getError();
        return 1;
    }

    //find if there were any errors while loading the files
    if(load_files() == false)
    {
        getError();
        return 1;
    }

    //apply images
    apply_surface(0, 0, background, screen);
    apply_surface(0, 0, sprite, screen);

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

    //while the user hasn't quit...
    while(done == false)
    {
        //and while there is events to handle...
        while(SDL_PollEvent(&event))
        {
            //if the user quit
            if(event.type == SDL_QUIT)
            {
                //set done to true to break the loop
                done = true;
            }
        }
    }

    //free surfaces and quit SDL
    clean_up();
    //return 0 to end the program
    return 0;
}

Re: Process terminated with status -1073741819

Posted: Mon Jun 15, 2009 8:02 pm
by rolland
Well, I'm at a loss. Time to sit back and wait for the knowledgeable to swoop in and offer some advice. In the meantime, you could abuse your logger and start logging checkpoints throughout your main function. Or use a real debugger...

Re: Process terminated with status -1073741819

Posted: Mon Jun 15, 2009 8:03 pm
by XianForce
hehe, I should learn how to use my debugger =p.

Re: Process terminated with status -1073741819

Posted: Mon Jun 15, 2009 8:20 pm
by rolland
Me too.

Re: Process terminated with status -1073741819

Posted: Mon Jun 15, 2009 8:25 pm
by XianForce
This is where Google comes in handy =p

Re: Process terminated with status -1073741819

Posted: Mon Jun 15, 2009 9:12 pm
by dandymcgee
XianForce wrote:This is where Google comes in handy =p
I use Code::Blocks, and running in debug mode is quite simple (a matter of click a different button to execute). It's awesome because when the program crashes it displays the call stack and in many cases you can use that to narrow the crash down to a single function if not a single line. There are of course exceptions, most notably (from personal experience) is when I start referencing garbage pointers and getting "status 3" (not so much fun to debug).

In short: Learn how to use your debugger you will NOT regret it.

SegFault with SDL_DisplayFormat?

Posted: Tue Jun 16, 2009 12:27 am
by XianForce
dandymcgee wrote:
XianForce wrote:This is where Google comes in handy =p
I use Code::Blocks, and running in debug mode is quite simple (a matter of click a different button to execute). It's awesome because when the program crashes it displays the call stack and in many cases you can use that to narrow the crash down to a single function if not a single line. There are of course exceptions, most notably (from personal experience) is when I start referencing garbage pointers and getting "status 3" (not so much fun to debug).

In short: Learn how to use your debugger you will NOT regret it.
Yeah I use Code::Blocks too... I found the problem, but I still have no idea of how to fix it...

*Changed thread topic*

Re: SegFault with SDL_DisplayFormat?

Posted: Tue Jun 16, 2009 1:23 am
by Joeyotrevor

Code: Select all

 if(init == false)
    {
        getError();
        return 1;
    }
You don't seem to have any parentheses after init, so I would guess it is comparing the memory address of init to false. That shouldn't even compile.

Re: SegFault with SDL_DisplayFormat?

Posted: Tue Jun 16, 2009 7:37 am
by XianForce
holy shit... avansc was right, I didn't initialize it right...

and that fixed it...

Thanks so much =D

Re: SegFault with SDL_DisplayFormat?

Posted: Tue Jun 16, 2009 1:48 pm
by MarauderIIC
So it's solved? Can you mark your topic "[SOLVED]"? (and maybe explain exactly what the problem & solution was, for archival purposes?)

Re: SegFault with SDL_DisplayFormat?

Posted: Wed Jun 17, 2009 10:22 am
by XianForce
Yes, will do so =D