Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a worker thread crash caused by the main thread releasing the c… #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 20 additions & 21 deletions src/raudio.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ void SetAudioBufferVolume(AudioBuffer *buffer, float volume);
void SetAudioBufferPitch(AudioBuffer *buffer, float pitch);
void SetAudioBufferPan(AudioBuffer *buffer, float pan);
void TrackAudioBuffer(AudioBuffer *buffer);
void UntrackAudioBuffer(AudioBuffer *buffer);
void UntrackAudioBuffer(AudioBuffer *buffer, bool freeSampleData);

//----------------------------------------------------------------------------------
// Module Functions Definition - Audio Device initialization and Closing
Expand Down Expand Up @@ -610,13 +610,7 @@ AudioBuffer *LoadAudioBuffer(ma_format format, ma_uint32 channels, ma_uint32 sam
// Delete an audio buffer
void UnloadAudioBuffer(AudioBuffer *buffer)
{
if (buffer != NULL)
{
ma_data_converter_uninit(&buffer->converter, NULL);
UntrackAudioBuffer(buffer);
RL_FREE(buffer->data);
RL_FREE(buffer);
}
UntrackAudioBuffer(buffer, true);
}

// Check if an audio buffer is playing
Expand Down Expand Up @@ -720,18 +714,28 @@ void TrackAudioBuffer(AudioBuffer *buffer)
}

// Untrack audio buffer from linked list
void UntrackAudioBuffer(AudioBuffer *buffer)
void UntrackAudioBuffer(AudioBuffer *buffer, bool freeSampleData)
{
ma_mutex_lock(&AUDIO.System.lock);
{
if (buffer->prev == NULL) AUDIO.Buffer.first = buffer->next;
else buffer->prev->next = buffer->next;
if (NULL != buffer)
{
ma_data_converter_uninit(&buffer->converter, NULL);

if (buffer->prev == NULL) AUDIO.Buffer.first = buffer->next;
else buffer->prev->next = buffer->next;

if (buffer->next == NULL) AUDIO.Buffer.last = buffer->prev;
else buffer->next->prev = buffer->prev;
if (buffer->next == NULL) AUDIO.Buffer.last = buffer->prev;
else buffer->next->prev = buffer->prev;

buffer->prev = NULL;
buffer->next = NULL;
buffer->prev = NULL;
buffer->next = NULL;

if (freeSampleData && NULL != buffer->data)
RL_FREE(buffer->data);

RL_FREE(buffer);
}
}
ma_mutex_unlock(&AUDIO.System.lock);
}
Expand Down Expand Up @@ -991,12 +995,7 @@ void UnloadSound(Sound sound)
void UnloadSoundAlias(Sound alias)
{
// Untrack and unload just the sound buffer, not the sample data, it is shared with the source for the alias
if (alias.stream.buffer != NULL)
{
ma_data_converter_uninit(&alias.stream.buffer->converter, NULL);
UntrackAudioBuffer(alias.stream.buffer);
RL_FREE(alias.stream.buffer);
}
UntrackAudioBuffer(alias.stream.buffer, false);
}

// Update sound buffer with new data
Expand Down