Fixed an issue where AudioManager::deinit() could cause a crash

This commit is contained in:
Leon Styhre 2024-05-19 18:42:29 +02:00
parent 7cc901bb7f
commit b9805053a9
2 changed files with 10 additions and 6 deletions

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
// //
// ES-DE // ES-DE Frontend
// AudioManager.cpp // AudioManager.cpp
// //
// Low-level audio functions (using SDL2). // Low-level audio functions (using SDL2).
@ -113,6 +113,9 @@ void AudioManager::init()
void AudioManager::deinit() void AudioManager::deinit()
{ {
if (sAudioDevice == 0)
return;
SDL_LockAudioDevice(sAudioDevice); SDL_LockAudioDevice(sAudioDevice);
SDL_FreeAudioStream(sConversionStream); SDL_FreeAudioStream(sConversionStream);
SDL_UnlockAudioDevice(sAudioDevice); SDL_UnlockAudioDevice(sAudioDevice);
@ -120,6 +123,7 @@ void AudioManager::deinit()
SDL_CloseAudio(); SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO); SDL_QuitSubSystem(SDL_INIT_AUDIO);
sConversionStream = nullptr;
sAudioDevice = 0; sAudioDevice = 0;
} }
@ -132,7 +136,7 @@ void AudioManager::mixAudio(void* /*unused*/, Uint8* stream, int len)
SDL_memset(stream, 0, len); SDL_memset(stream, 0, len);
// Iterate through all our samples. // Iterate through all our samples.
std::vector<std::shared_ptr<Sound>>::const_iterator soundIt = sSoundVector.cbegin(); std::vector<std::shared_ptr<Sound>>::const_iterator soundIt {sSoundVector.cbegin()};
while (soundIt != sSoundVector.cend()) { while (soundIt != sSoundVector.cend()) {
std::shared_ptr<Sound> sound {*soundIt}; std::shared_ptr<Sound> sound {*soundIt};
if (sound->isPlaying()) { if (sound->isPlaying()) {

View file

@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
// //
// ES-DE // ES-DE Frontend
// AudioManager.h // AudioManager.h
// //
// Low-level audio functions (using SDL2). // Low-level audio functions (using SDL2).
@ -49,10 +49,10 @@ private:
static void mixAudio(void* unused, Uint8* stream, int len); static void mixAudio(void* unused, Uint8* stream, int len);
static inline SDL_AudioStream* sConversionStream; static inline SDL_AudioStream* sConversionStream {nullptr};
static inline std::vector<std::shared_ptr<Sound>> sSoundVector; static inline std::vector<std::shared_ptr<Sound>> sSoundVector;
static inline std::atomic<bool> sMuteStream = false; static inline std::atomic<bool> sMuteStream {false};
static inline bool sHasAudioDevice = true; static inline bool sHasAudioDevice {true};
}; };
#endif // ES_CORE_AUDIO_MANAGER_H #endif // ES_CORE_AUDIO_MANAGER_H