Getting a segfault trying to check the format of image loaded with SDL

94 Views Asked by At

I'm trying to load an image with SDL2, I've initialized SDL_Image with IMG_INIT_PNG|IMG_INIT_JPG, and I'm trying to open this PNG as so:

Perlin noise

typedef std::unique_ptr<SDL_Surface, SDLDeleter> SDL_Surfacep;

SDL_Surfacep LoadImageFromFile(std::string_view mime_type, std::string const& path, const uint8_t * data, uint32_t length)
{
    SDL_RWops * rwOps;

    if(data == nullptr)
        rwOps = (SDL_RWFromFile(path.c_str(), "rb"));
    else
        rwOps = (SDL_RWFromConstMem(data, length));

    if(rwOps == nullptr)
        throw std::runtime_error("failed to open data stream...");

    SDL_Surfacep image(IMG_Load_RW(rwOps, 1));

    if(image == nullptr || image->format == nullptr)
    {
        if(data == nullptr)
            throw std::runtime_error(cc("failed to load image ", path, " of type ", mime_type, ",", SDL_GetError()));
        else
            throw std::runtime_error(cc("failed to load ", mime_type, " file in container ", path, ",", SDL_GetError()));

        return {};
    }

// SIGSEV
    int format = image->format->format;

    return image;
}

The program crashes where I've put the SIGSEV comment with a segmentation fault, and then according to the debugger the image struct has this contents:

Debugger contents

In addition to the format and pixels being bad pointers the width and pitch also appear to be wrong. I have no idea how to fix this at this point.

1

There are 1 best solutions below

1
Patrick Jeeves On

It was because code i had copy/pasted into a header file contained a #pragma push command; and that completely broke library calls.