-1

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.

Patrick Jeeves
  • 371
  • 2
  • 16
  • the obvious thing is that 0x588dd1f00000000 is not a valid pointer. It looks a little suspicious with all those 00000 at the end – pm100 Mar 21 '23 at 23:27
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Jesper Juhl Mar 22 '23 at 00:46

1 Answers1

-2

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

Patrick Jeeves
  • 371
  • 2
  • 16
  • 1
    In other words, the question is unanswerable because it lacks key information; it does not provide a [mre]. – JaMiT Mar 22 '23 at 03:06