diff --git a/Runtime/Scripts/AudioFrame.cs b/Runtime/Scripts/AudioFrame.cs index 3aa450c8..5b3441a5 100644 --- a/Runtime/Scripts/AudioFrame.cs +++ b/Runtime/Scripts/AudioFrame.cs @@ -23,6 +23,7 @@ public class AudioFrame : IDisposable public AudioFrameBufferInfo Info => _info; + private NativeArray _allocatedData; // Only used if the frame's data is allocated by Unity private IntPtr _dataPtr; public IntPtr Data => _dataPtr; @@ -44,8 +45,8 @@ internal AudioFrame(uint sampleRate, uint numChannels, uint samplesPerChannel) { _samplesPerChannel = samplesPerChannel; unsafe { - var data = new NativeArray(Length, Allocator.Persistent); - _dataPtr = (IntPtr)NativeArrayUnsafeUtility.GetUnsafePtr(data); + _allocatedData = new NativeArray(Length, Allocator.Persistent); + _dataPtr = (IntPtr)NativeArrayUnsafeUtility.GetUnsafePtr(_allocatedData); } } ~AudioFrame() @@ -63,6 +64,10 @@ protected virtual void Dispose(bool disposing) { if (!_disposed) { + if (_allocatedData.IsCreated) + { + _allocatedData.Dispose(); + } _disposed = true; } }