Unity-Technologies / com.unity.webrtc

WebRTC package for Unity
Other
738 stars 185 forks source link

fix: Fix GCAlloc #985

Open karasusan opened 9 months ago

aet commented 8 months ago

Seeing this in the editor occasionally on script reload:

A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeArray`1:.ctor(Int32, Allocator, NativeArrayOptions) (at /Users/bokken/build/output/unity/unity/Runtime/Export/NativeArray/NativeArray.cs:58)
Unity.WebRTC.Batch:.ctor() (at Library/PackageCache/com.unity.webrtc@642801c9b1/Runtime/Scripts/Context.cs:75)
Unity.WebRTC.Context:.ctor(IntPtr, Int32) (at Library/PackageCache/com.unity.webrtc@642801c9b1/Runtime/Scripts/Context.cs:151)
Unity.WebRTC.Context:Create(Int32) (at Library/PackageCache/com.unity.webrtc@642801c9b1/Runtime/Scripts/Context.cs:138)
Unity.WebRTC.WebRTC:InitializeInternal(Boolean, Boolean, NativeLoggingSeverity) (at Library/PackageCache/com.unity.webrtc@642801c9b1/Runtime/Scripts/WebRTC.cs:736)
Unity.WebRTC.ContextManager:OnAfterAssemblyReload() (at Library/PackageCache/com.unity.webrtc@642801c9b1/Runtime/Scripts/Context.cs:33)
UnityEditor.AssemblyReloadEvents:OnAfterAssemblyReload() (at /Users/bokken/build/output/unity/unity/Editor/Mono/AssemblyReloadEvents.cs:34)

Fixed by

diff --git a/Runtime/Scripts/Context.cs b/Runtime/Scripts/Context.cs
index c8c49f0..3aa38f4 100644
--- a/Runtime/Scripts/Context.cs
+++ b/Runtime/Scripts/Context.cs
@@ -175,6 +175,7 @@ namespace Unity.WebRTC

                 // Release buffers on the rendering thread
                 batch.Submit(true);
+                batch.Dispose();

                 NativeMethods.ContextDestroy(id);
                 self = IntPtr.Zero;