Skip to content

Commit 6ec693c

Browse files
committed
Fix use-after-free crash in audio synthesis on level reset (#133)
1 parent 554c653 commit 6ec693c

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/audio/external.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2622,7 +2622,11 @@ void play_toads_jingle(void) {
26222622
/**
26232623
* Called from threads: thread5_game_loop
26242624
*/
2625+
void GameEngine_LockAudioThread(void);
2626+
void GameEngine_UnlockAudioThread(void);
2627+
26252628
void sound_reset(u8 presetId) {
2629+
GameEngine_LockAudioThread();
26262630
#ifndef VERSION_JP
26272631
if (presetId >= 8) {
26282632
presetId = 0;
@@ -2651,6 +2655,7 @@ void sound_reset(u8 presetId) {
26512655
D_80332108 = (D_80332108 & 0xf0) + presetId;
26522656
gSoundMode = D_80332108 >> 4;
26532657
sHasStartedFadeOut = FALSE;
2658+
GameEngine_UnlockAudioThread();
26542659
}
26552660

26562661
/**

src/port/Engine.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,14 @@ void GameEngine::EndAudioFrame() {
11821182
}
11831183
}
11841184

1185+
extern "C" void GameEngine_LockAudioThread() {
1186+
audio.mutex.lock();
1187+
}
1188+
1189+
extern "C" void GameEngine_UnlockAudioThread() {
1190+
audio.mutex.unlock();
1191+
}
1192+
11851193
void GameEngine::AudioInit() {
11861194
const auto resourceMgr = Ship::Context::GetInstance()->GetResourceManager();
11871195
resourceMgr->LoadResources("sound");

0 commit comments

Comments
 (0)