From 08dd1e61b6d6c2efe4de1c6c7992b81dcd822596 Mon Sep 17 00:00:00 2001 From: Iliyas Jorio Date: Wed, 23 Feb 2022 23:20:47 +0100 Subject: [PATCH] Fix memory leak in SndStartFilePlay --- src/SoundMixer/SoundManager.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/SoundMixer/SoundManager.cpp b/src/SoundMixer/SoundManager.cpp index e1e6ad1..a287ca8 100644 --- a/src/SoundMixer/SoundManager.cpp +++ b/src/SoundMixer/SoundManager.cpp @@ -119,7 +119,7 @@ OSErr SndChannelStatus(SndChannelPtr chan, short theLength, SCStatusPtr theStatu } // Install a sampled sound as a voice in a channel. -static void InstallSoundInChannel(SndChannelPtr chan, const Ptr sampledSoundHeader) +static void InstallSoundInChannel(SndChannelPtr chan, const Ptr sampledSoundHeader, bool forceCopy=false) { //--------------------------------- // Get internal channel @@ -146,6 +146,12 @@ static void InstallSoundInChannel(SndChannelPtr chan, const Ptr sampledSoundHead codec->Decode(info.nChannels, spanIn, spanOut); impl.source.Init(info.sampleRate, 16, info.nChannels, false, spanOut); } + else if (forceCopy) + { + auto spanOut = impl.source.GetBuffer(info.decompressedLength); + memcpy(spanOut.data(), spanIn.data(), spanIn.size()); + impl.source.Init(info.sampleRate, info.codecBitDepth, info.nChannels, info.bigEndian, spanOut); + } else { impl.source.Init(info.sampleRate, info.codecBitDepth, info.nChannels, info.bigEndian, spanIn); @@ -313,7 +319,9 @@ OSErr SndStartFilePlay( SndListHandle sndListHandle = Pomme_SndLoadFileAsResource(fRefNum); long offset = 0; GetSoundHeaderOffset(sndListHandle, &offset); - InstallSoundInChannel(chan, ((Ptr) *sndListHandle) + offset); + InstallSoundInChannel(chan, ((Ptr) *sndListHandle) + offset, true); + DisposeHandle((Handle) sndListHandle); + sndListHandle = nullptr; auto& impl = GetChannelImpl(chan); if (theCompletion)