Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Mix_LoadMUS_RW causes crash #571

Closed
huabanlu opened this issue Oct 27, 2023 · 1 comment
Closed

Using Mix_LoadMUS_RW causes crash #571

huabanlu opened this issue Oct 27, 2023 · 1 comment

Comments

@huabanlu
Copy link

Source code from
https://lazyfoo.net/tutorials/SDL/21_sound_effects_and_music/index.php

I want to change

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

to

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

This will cause it to crash.

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

version:
SDL2-2.28.0
SDL2_mixer-2.6.3

@madebr
Copy link
Contributor

madebr commented Oct 27, 2023

Duplicate of libsdl-org/SDL#8440

@madebr madebr marked this as a duplicate of libsdl-org/SDL#8440 Oct 27, 2023
@madebr madebr closed this as completed Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants