From 8535658791fc7aa0f9202b0a7837dd67c4ad970d Mon Sep 17 00:00:00 2001 From: Deve Date: Mon, 7 Nov 2022 10:57:20 +0100 Subject: [PATCH] Use raw pointer for sound manager (#107) Use raw pointer for sound manager to avoid a crash in destructor after exit(0) --- src/client/clientlauncher.cpp | 9 ++++++--- src/client/game.cpp | 4 ++-- src/client/sound_openal.cpp | 15 +++++++++++---- src/client/sound_openal.h | 5 +++-- src/gui/guiEngine.cpp | 4 ++-- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/client/clientlauncher.cpp b/src/client/clientlauncher.cpp index c40484a33..f06b33379 100644 --- a/src/client/clientlauncher.cpp +++ b/src/client/clientlauncher.cpp @@ -80,7 +80,7 @@ ClientLauncher::~ClientLauncher() delete RenderingEngine::get_instance(); #if USE_SOUND - g_sound_manager_singleton.reset(); + deleteSoundManagerSingleton(g_sound_manager_singleton); #endif } @@ -101,8 +101,11 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args) return RenderingEngine::print_video_modes(); #if USE_SOUND - if (g_settings->getBool("enable_sound")) - g_sound_manager_singleton = createSoundManagerSingleton(); + if (g_settings->getBool("enable_sound")) { + // Check if it's already created just in case + if (!g_sound_manager_singleton) + g_sound_manager_singleton = createSoundManagerSingleton(); + } #endif if (!init_engine()) { diff --git a/src/client/game.cpp b/src/client/game.cpp index 2fab95e1c..e200a341d 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -1292,9 +1292,9 @@ bool Game::init( bool Game::initSound() { #if USE_SOUND - if (g_settings->getBool("enable_sound") && g_sound_manager_singleton.get()) { + if (g_settings->getBool("enable_sound") && g_sound_manager_singleton) { infostream << "Attempting to use OpenAL audio" << std::endl; - sound = createOpenALSoundManager(g_sound_manager_singleton.get(), &soundfetcher); + sound = createOpenALSoundManager(g_sound_manager_singleton, &soundfetcher); if (!sound) infostream << "Failed to initialize OpenAL audio" << std::endl; } else diff --git a/src/client/sound_openal.cpp b/src/client/sound_openal.cpp index 8a4366cae..434f3807f 100644 --- a/src/client/sound_openal.cpp +++ b/src/client/sound_openal.cpp @@ -50,7 +50,7 @@ with this program; ifnot, write to the Free Software Foundation, Inc., #define BUFFER_SIZE 30000 -std::shared_ptr g_sound_manager_singleton; +SoundManagerSingleton* g_sound_manager_singleton = nullptr; typedef std::unique_ptr unique_ptr_alcdevice; typedef std::unique_ptr unique_ptr_alccontext; @@ -707,15 +707,22 @@ public: } }; -std::shared_ptr createSoundManagerSingleton() +SoundManagerSingleton* createSoundManagerSingleton() { - auto smg = std::make_shared(); + auto smg = new SoundManagerSingleton(); if (!smg->init()) { - smg.reset(); + delete smg; + smg = nullptr; } return smg; } +void deleteSoundManagerSingleton(SoundManagerSingleton* smg) +{ + delete smg; + smg = nullptr; +} + ISoundManager *createOpenALSoundManager(SoundManagerSingleton *smg, OnDemandSoundFetcher *fetcher) { return new OpenALSoundManager(smg, fetcher); diff --git a/src/client/sound_openal.h b/src/client/sound_openal.h index b48ecb189..d50bad4e3 100644 --- a/src/client/sound_openal.h +++ b/src/client/sound_openal.h @@ -24,8 +24,9 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "sound.h" class SoundManagerSingleton; -extern std::shared_ptr g_sound_manager_singleton; +extern SoundManagerSingleton* g_sound_manager_singleton; -std::shared_ptr createSoundManagerSingleton(); +SoundManagerSingleton* createSoundManagerSingleton(); +void deleteSoundManagerSingleton(SoundManagerSingleton* smg); ISoundManager *createOpenALSoundManager( SoundManagerSingleton *smg, OnDemandSoundFetcher *fetcher); diff --git a/src/gui/guiEngine.cpp b/src/gui/guiEngine.cpp index 78a47bc3e..76528c7d3 100644 --- a/src/gui/guiEngine.cpp +++ b/src/gui/guiEngine.cpp @@ -150,8 +150,8 @@ GUIEngine::GUIEngine(JoystickController *joystick, //create soundmanager MenuMusicFetcher soundfetcher; #if USE_SOUND - if (g_settings->getBool("enable_sound") && g_sound_manager_singleton.get()) - m_sound_manager = createOpenALSoundManager(g_sound_manager_singleton.get(), &soundfetcher); + if (g_settings->getBool("enable_sound") && g_sound_manager_singleton) + m_sound_manager = createOpenALSoundManager(g_sound_manager_singleton, &soundfetcher); #endif if (!m_sound_manager) m_sound_manager = &dummySoundManager;