From ee7ef810541d23b7b3abc63aeb52e32ec3a81fb1 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 15 Jul 2022 15:21:58 -0400 Subject: [PATCH] Avoid potential attempt to free enqueued buffers at dealloc. --- OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m b/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m index bc741a4c6..e6baa6c95 100644 --- a/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m +++ b/OSBindings/Mac/Clock Signal/Audio/CSAudioQueue.m @@ -47,7 +47,11 @@ if(self) { _deallocLock = [[NSLock alloc] init]; + _deallocLock.name = @"Dealloc lock"; + _queueLock = [[NSLock alloc] init]; + _queueLock.name = @"Audio queue access lock"; + atomic_store_explicit(&_enqueuedBuffers, 0, memory_order_relaxed); _samplingRate = samplingRate; @@ -115,6 +119,12 @@ - (void)dealloc { [_deallocLock lock]; + // Ensure no buffers remain enqueued by stopping the queue. + if(_audioQueue) { + OSSGuard(AudioQueueStop(_audioQueue, true)); + } + + // Free all buffers. for(size_t c = 0; c < NumBuffers; c++) { if(_buffers[c]) { OSSGuard(AudioQueueFreeBuffer(_audioQueue, _buffers[c])); @@ -122,6 +132,7 @@ } } + // Dispose of the queue. if(_audioQueue) { OSSGuard(AudioQueueDispose(_audioQueue, true)); _audioQueue = NULL;