Strange unhandled exception; Access violation reading location

1

When I try to call SDL_DisplayFormatAlpha, I get this unhandled exception: "Unhandled exception at 0x68125981 (SDL.dll) in herorpg.exe: 0xC0000005: Access violation reading location 0x0000013C."

Code:

SDL_Surface* load_image ( char* filename ) {
    SDL_Surface* loadedImage = NULL;
    SDL_Surface* optimizedImage = NULL;
    loadedImage = IMG_Load ( filename );

    if ( loadedImage != NULL ) {
        optimizedImage = SDL_DisplayFormatAlpha ( loadedImage );
        SDL_FreeSurface ( loadedImage );
    }

    return optimizedImage;
}

bool init ( SDL_Surface*& screen ) {

    //Initialize SDL
    if ( SDL_Init ( SDL_INIT_EVERYTHING ) == -1 ) return false;
    screen = SDL_SetVideoMode ( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_FULLSCREEN );
    if ( screen == NULL ) return false;

    //Set the caption
    SDL_WM_SetCaption ( "Hero RPG", NULL );

    return true;
}

Main:

int main ( int argc, char** argv ) {
init ( screen );

load_image ( "graphics\\background\\overworld" );

return 0;
}

Other:

If I comment out

    //if ( loadedImage != NULL ) {
    //  optimizedImage = SDL_DisplayFormatAlpha ( loadedImage );
    //  SDL_FreeSurface ( loadedImage );
    //}

and change

    loadedImage = IMG_Load ( filename );

to

    optimizedImage = IMG_Load ( filename );

the image loads fine. So I know there is no problem with the file name.

Question:

This is the exact function I used in the last program I designed, and it worked fine last time. I think the answer to this will be outside of the box. Why would I be receiving this unhandled exception?

c++
sdl
asked on Stack Overflow May 8, 2013 by James Hurley • edited May 18, 2013 by James Hurley

1 Answer

1

In the documentation it has this line:

Of course, the video surface must be initialised using SDL_SetVideoMode before this function is called, or it will segfault.

Seeing I don't see a call to SDL_SetVideoMode in your posted code, I can assume this is probably the cause.

answered on Stack Overflow May 8, 2013 by Tony The Lion

User contributions licensed under CC BY-SA 3.0