Search code examples
c++exceptionvisual-studio-2013access-violationsdl-2

Loading images with C++/SDL2 - "unhandled exception", "access violation reading location"


I was studying someone else's Pong code for an earlier version of SDL, and I'm trying to get it running with SDL2. Below is an excerpt from my code, which only loads the images. When this code is run, I get a window popping up telling me this: Unhandled exception at 0x00A065AD in The Pong.exe: 0xC0000005: Access violation reading location 0x00000004. While it's debugging, it also points to this line:

optimizedImage = SDL_ConvertSurface(loadedImage, windowSurface->format, 0);

The error list shows warning LNK4098: defaultlib 'msvcrt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library.

I'm not sure what to make of all this. On top of that, the SDL window shows up, freezes, and sometimes won't close even after debugging has ended. (It tells me there's an access violation when I try.)

#include <string>
#include <SDL.h>

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 475;
const int SCREEN_BPP = 2;

SDL_Window *window = NULL;
SDL_Surface *windowSurface = NULL;
SDL_Surface *ball = NULL;
SDL_Surface *background = NULL;
SDL_Surface *paddleL = NULL;
SDL_Surface *paddleR = NULL;

SDL_Event event;

SDL_Surface *loadImage(std::string fileName)
{
    SDL_Surface *loadedImage = NULL;
    SDL_Surface *optimizedImage = NULL;

    loadedImage = SDL_LoadBMP(fileName.c_str());

    if (loadedImage != NULL)
    {
        optimizedImage = SDL_ConvertSurface(loadedImage, windowSurface->format, 0);

        SDL_FreeSurface(loadedImage);

        if (optimizedImage != NULL)
        {
            SDL_SetColorKey(optimizedImage, SDL_TRUE,
                            SDL_MapRGB(optimizedImage->format, 255, 255, 255));
        }
    }

    return optimizedImage;
}

bool init()
{
    if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
        return false;

    window = SDL_CreateWindow("Pong", SDL_WINDOWPOS_CENTERED,
                            SDL_WINDOWPOS_CENTERED, WINDOW_WIDTH,
                            WINDOW_HEIGHT, SDL_WINDOW_SHOWN);

    if (window == NULL)
        return false;

    return true;
}

bool loadFiles()
{
    ball = loadImage("ball.bmp");
    background = loadImage("background.bmp");
    paddleL = loadImage("paddleRed.bmp");
    paddleR = loadImage("paddleBlue.bmp");

    if (ball == NULL)
        return false;

    if (background == NULL)
        return false;

    if (paddleL == NULL || paddleR == NULL)
        return false;

    return true;
}

int main(int argc, char* args[])
{
    windowSurface = SDL_GetWindowSurface(window);

    if (init() == false)
        return 1;
    if (loadFiles() == false)
        return 1;

    SDL_FreeSurface(windowSurface);
    SDL_FreeSurface(ball);
    SDL_FreeSurface(background);
    SDL_FreeSurface(paddleL);
    SDL_FreeSurface(paddleR);
    windowSurface = ball = background = paddleL = paddleR = nullptr;
    SDL_DestroyWindow(window);
    window = nullptr;

    SDL_Quit();
    return 0;
}

Solution

  • You are calling

    windowSurface = SDL_GetWindowSurface(window);
    

    before

    window = SDL_CreateWindow("Pong", SDL_WINDOWPOS_CENTERED,
    

    so window will still be NULL, causing windowSurface to be NULL, causing windowSurface->format to cause a crash.