[Solved] SegFault with SDL_DisplayFormat?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

[Solved] SegFault with SDL_DisplayFormat?

Post 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.
Last edited by XianForce on Wed Jun 17, 2009 10:23 am, edited 2 times in total.
User avatar
rolland
Chaos Rift Regular
Chaos Rift Regular
Posts: 127
Joined: Fri Dec 21, 2007 2:27 pm
Current Project: Starting an Android app soon
Favorite Gaming Platforms: PS1, N64
Programming Language of Choice: C++
Location: Michigan, US

Re: Process terminated with status -1073741819

Post 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.
I'll write a signature once I get some creativity and inspiration...
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Process terminated with status -1073741819

Post 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;
}
User avatar
rolland
Chaos Rift Regular
Chaos Rift Regular
Posts: 127
Joined: Fri Dec 21, 2007 2:27 pm
Current Project: Starting an Android app soon
Favorite Gaming Platforms: PS1, N64
Programming Language of Choice: C++
Location: Michigan, US

Re: Process terminated with status -1073741819

Post 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...
I'll write a signature once I get some creativity and inspiration...
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Process terminated with status -1073741819

Post by XianForce »

hehe, I should learn how to use my debugger =p.
User avatar
rolland
Chaos Rift Regular
Chaos Rift Regular
Posts: 127
Joined: Fri Dec 21, 2007 2:27 pm
Current Project: Starting an Android app soon
Favorite Gaming Platforms: PS1, N64
Programming Language of Choice: C++
Location: Michigan, US

Re: Process terminated with status -1073741819

Post by rolland »

Me too.
I'll write a signature once I get some creativity and inspiration...
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Process terminated with status -1073741819

Post by XianForce »

This is where Google comes in handy =p
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Process terminated with status -1073741819

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

SegFault with SDL_DisplayFormat?

Post 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*
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

Re: SegFault with SDL_DisplayFormat?

Post 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.

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: SegFault with SDL_DisplayFormat?

Post by XianForce »

holy shit... avansc was right, I didn't initialize it right...

and that fixed it...

Thanks so much =D
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: SegFault with SDL_DisplayFormat?

Post by MarauderIIC »

So it's solved? Can you mark your topic "[SOLVED]"? (and maybe explain exactly what the problem & solution was, for archival purposes?)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: SegFault with SDL_DisplayFormat?

Post by XianForce »

Yes, will do so =D
Post Reply