c++ 尝试检查SDL加载的图像格式时出现segfault

ddhy6vgd  于 2023-03-25  发布在  其他
关注(0)|答案(1)|浏览(112)

我尝试用SDL2加载图像,我已经用IMG_INIT_PNG初始化了SDL_Image|IMG_INIT_JPG,我尝试打开这个PNG如下:

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;
}

程序崩溃的地方我已经把SIGSEV注解与分段错误,然后根据调试器的图像结构有这样的内容:

除了格式和像素是坏指针,宽度和间距也似乎是错误的。我不知道如何修复这一点在这一点上。

toiithl6

toiithl61#

这是因为我复制/粘贴到头文件中的代码包含一个#pragma push命令;这完全中断了图书馆的呼叫。

相关问题