Skip to content

Commit 7c6f099

Browse files
committed
Fix memory leak
Allocated audio frame data is not disposed of when allocated by Unity
1 parent 787ff31 commit 7c6f099

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Runtime/Scripts/AudioFrame.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class AudioFrame : IDisposable
2323

2424
public AudioFrameBufferInfo Info => _info;
2525

26+
private NativeArray<byte> _allocatedData; // Only used if the frame's data is allocated by Unity
2627
private IntPtr _dataPtr;
2728
public IntPtr Data => _dataPtr;
2829

@@ -44,8 +45,8 @@ internal AudioFrame(uint sampleRate, uint numChannels, uint samplesPerChannel) {
4445
_samplesPerChannel = samplesPerChannel;
4546
unsafe
4647
{
47-
var data = new NativeArray<byte>(Length, Allocator.Persistent);
48-
_dataPtr = (IntPtr)NativeArrayUnsafeUtility.GetUnsafePtr(data);
48+
_allocatedData = new NativeArray<byte>(Length, Allocator.Persistent);
49+
_dataPtr = (IntPtr)NativeArrayUnsafeUtility.GetUnsafePtr(_allocatedData);
4950
}
5051
}
5152
~AudioFrame()
@@ -63,6 +64,10 @@ protected virtual void Dispose(bool disposing)
6364
{
6465
if (!_disposed)
6566
{
67+
if (_allocatedData.IsCreated)
68+
{
69+
_allocatedData.Dispose();
70+
}
6671
_disposed = true;
6772
}
6873
}

0 commit comments

Comments
 (0)