使用Mix_LoadMUS_RW会导致崩溃

Using Mix_LoadMUS_RW causes crash

提问人:huabanlu 提问时间:10/27/2023 最后编辑:genpfaulthuabanlu 更新时间:10/27/2023 访问量:30

问:

源代码从这里

我想改变

gMusic = Mix_LoadMUS("../test/asset/bgm1.wav");

std::vector<unsigned char> data; 
if (!readFileToMemory("../test/asset/bgm1.wav", data))
{
    printf("readFileToMemory error\n");
}
SDL_RWops *rwops = SDL_RWFromConstMem(data.data(), static_cast<int>(data.size()));
// gMusic = Mix_LoadMUSType_RW(rwops, Mix_MusicType::MUS_WAV, 1);
gMusic = Mix_LoadMUS_RW(rwops,1);

这将导致它崩溃。

readFileToMemory 的代码:

bool readFileToMemory(const std::string &filePath, std::vector<unsigned char> &data)
{
    std::ifstream file(filePath, std::ios::binary | std::ios::ate);

    if (!file.is_open())
    {
        std::cerr << "Failed to open file: " << filePath << std::endl;
        return false;
    }

    std::streampos fileSize = file.tellg();
    if (fileSize <= 0)
    {
        std::cerr << "File is empty: " << filePath << std::endl;
        file.close();
        return false;
    }

    data.resize(static_cast<size_t>(fileSize));

    file.seekg(0, std::ios::beg);
    file.read(reinterpret_cast<char *>(data.data()), fileSize);
    file.close();

    return true;
}

版本:

  • SDL2-2.28.0
  • SDL2_mixer-2.6.3
C++ SDL IOstream

评论


答: 暂无答案