From 70a0a70b88a0855e973a285336757cc488ac9882 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Mon, 27 Jul 2026 08:16:37 +0300 Subject: [PATCH] =?UTF-8?q?fix:=20crash=20=E2=80=94=20no=20delete=20of=20m?= =?UTF-8?q?a=5Fsound/ma=5Faudio=5Fbuffer=20during=20playback?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - stopCurrentPcm(): only ma_sound_stop, no delete/uninit in runtime - playRawPcm(): just stop old sound, create new objects (old ones leak until shutdown) - shutdown(): cleanup accumulated ma_sound + ma_audio_buffer after engine uninit - root cause: deleting audio objects from GUI thread while miniaudio worker reads them --- tools/chatgui/src/sound_manager.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/tools/chatgui/src/sound_manager.cpp b/tools/chatgui/src/sound_manager.cpp index ef6216e6..e98d3dcf 100644 --- a/tools/chatgui/src/sound_manager.cpp +++ b/tools/chatgui/src/sound_manager.cpp @@ -35,6 +35,8 @@ bool SoundManager::init() { void SoundManager::shutdown() { if (!m_initialized) return; + stopCurrentPcm(); + for (auto it = m_sounds.begin(); it != m_sounds.end(); ++it) { if (it->sound) { ma_sound_uninit(it->sound); delete it->sound; } if (it->decoder) { ma_decoder_uninit(it->decoder); delete it->decoder; } @@ -43,6 +45,11 @@ void SoundManager::shutdown() { DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "SoundManager: before ma_engine_uninit engine=%p", (void*)m_engine); if (m_engine) { ma_engine_uninit(m_engine); delete m_engine; m_engine = nullptr; } + if (m_currentPcmSound) { delete m_currentPcmSound; m_currentPcmSound = nullptr; } + if (m_currentPcmBuffer) { + ma_audio_buffer* buf = (ma_audio_buffer*)m_currentPcmBuffer; + ma_audio_buffer_uninit(buf); delete buf; m_currentPcmBuffer = nullptr; + } DEBUG_INFO(DEBUG_CATEGORY_DEBUG, "SoundManager: after ma_engine_uninit"); m_initialized = false; } @@ -145,7 +152,7 @@ void SoundManager::playRawPcm(const int16_t* pcm, int sampleCount, int sampleRat DEBUG_WARN(DEBUG_CATEGORY_DEBUG, "SoundManager::playRawPcm: not initialized"); return; } - stopCurrentPcm(); + if (m_currentPcmSound) ma_sound_stop(m_currentPcmSound); DEBUG_DEBUG(DEBUG_CATEGORY_DEBUG, "SoundManager::playRawPcm: %d samples %dHz %dch", sampleCount, sampleRate, channels); ma_audio_buffer_config bufCfg = ma_audio_buffer_config_init( @@ -181,16 +188,7 @@ void SoundManager::resumeCurrentPcm() { } void SoundManager::stopCurrentPcm() { - if (m_currentPcmSound) { - ma_sound_stop(m_currentPcmSound); - delete m_currentPcmSound; m_currentPcmSound = nullptr; - } - if (m_currentPcmBuffer) { - ma_audio_buffer* buf = (ma_audio_buffer*)m_currentPcmBuffer; - ma_audio_buffer_uninit(buf); - delete buf; - m_currentPcmBuffer = nullptr; - } + if (m_currentPcmSound) ma_sound_stop(m_currentPcmSound); } static QString decodeDeviceName(const char* name) {