Remove sources from the mixer before deleting them

This commit is contained in:
Iliyas Jorio 2023-01-21 13:22:39 +01:00
parent 53cbdb54c4
commit dae4b61b5b
3 changed files with 22 additions and 2 deletions

View File

@ -31,7 +31,7 @@ ChannelImpl::ChannelImpl(SndChannelPtr _macChannel, bool transferMacChannelOwner
ChannelImpl::~ChannelImpl()
{
Unlink();
Unlink(); // Unlink chan from list of managed chans
macChannel->channelImpl = nullptr;
@ -39,6 +39,11 @@ ChannelImpl::~ChannelImpl()
{
delete macChannel;
}
// Make sure we've stopped mixing the source before we allow its destructor
// to be called. Otherwise, the WavSource's buffer may be freed as it is still
// being processed!
source.RemoveFromMixer();
}
void ChannelImpl::Recycle()

View File

@ -255,16 +255,29 @@ void Source::Init(int theSampleRate, int theLength)
Stop();
}
Source::~Source()
void Source::RemoveFromMixer()
{
gMixer.Lock();
if (active)
{
gMixer.sources.remove(this);
active = false;
}
gMixer.Unlock();
}
Source::~Source()
{
if (active)
{
// You MUST call RemoveFromMixer before destroying a source. If you get here, your program is incorrect.
fprintf(stderr, "Source wasn't removed from mixer prior to destruction!\n");
#if _DEBUG
std::terminate();
#endif
}
}
void Source::Rewind()
{
RewindImplementation();

View File

@ -77,6 +77,8 @@ namespace cmixer
public:
virtual ~Source();
void RemoveFromMixer();
void Clear();
void Rewind();