-
Notifications
You must be signed in to change notification settings - Fork 447
A Native Collection has not been disposed, resulting in a memory leak #2862
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
Comments
This comment was marked as outdated.
This comment was marked as outdated.
Thank you for the explanation regarding the disposal of NetworkList. I understand the analogy to other IDisposable implementations in .NET. However, I have a couple of concerns: Firstly, there seems to be a lack of documentation on NetworkList needing explicit disposal. If this is an essential requirement, could you point me to where this is documented? Clear documentation would greatly aid in avoiding such issues. Secondly, the example provided in the NGO documentation does not explicitly call the Dispose method on NetworkList. This example (see below) led me to believe that disposal might be handled internally, especially since NetworkList cannot be initialised at declaration like NetworkVariable and must be initialized in Awake to avoid memory leaks. Here’s the snippet from the documentation:
Lastly, the stack trace from my issue originates from Unity.Netcode.Editor.NetworkBehaviourEditor:RenderNetworkVariable, suggesting the problem might be linked to Unity's handling of NetworkList in the editor rather than misuse of the class. Given these points, I would appreciate further clarification on the proper usage and disposal of NetworkList, as well as any adjustments that might be needed in the documentation or editor tooling to prevent such issues. |
Sorry for sloppy response. Indeed it seems that NetworkList is intended to be disposed automatically by NetworkBehavior.OnDestroy (just like NetworkVariable which seems to implements IDisposable too). Stacktrace is indeed suspicious. |
Description
I am using NetworkList's but I keep getting exceptions in the editor related to a memory leaks. I don't really have reproduction steps, but it seems to happen frequently. Often, I will make code changes during run time and then stop and restart my server and client to test and on startup the exceptions will occur.
I am initialising the NetworkList in awake and I always call base.OnDestroy in my overriden OnDestroy methods.
Environment
Additional Context
Exception Message:
A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeList
1:.ctor(Int32, AllocatorHandle) (at .\Library\PackageCache\[email protected]\Unity.Collections\NativeList.cs:116) Unity.Netcode.NetworkList
1:.ctor() (at .\Library\PackageCache\[email protected]\Runtime\NetworkVariable\Collections\NetworkList.cs:15)System.Reflection.RuntimeConstructorInfo:InternalInvoke(RuntimeConstructorInfo, Object, Object[], Exception&)
System.Reflection.RuntimeConstructorInfo:InternalInvoke(Object, Object[], Boolean)
System.RuntimeType:CreateInstanceMono(Boolean, Boolean)
System.RuntimeType:CreateInstanceSlow(Boolean, Boolean, Boolean, Boolean)
System.RuntimeType:CreateInstanceDefaultCtor(Boolean, Boolean, Boolean, Boolean, StackCrawlMark&)
System.Activator:CreateInstance(Type, Boolean, Boolean)
System.Activator:CreateInstance(Type, Boolean)
Unity.Netcode.Editor.NetworkBehaviourEditor:RenderNetworkVariable(Int32) (at .\Library\PackageCache\[email protected]\Editor\NetworkBehaviourEditor.cs:72)
Unity.Netcode.Editor.NetworkBehaviourEditor:OnInspectorGUI() (at .\Library\PackageCache\[email protected]\Editor\NetworkBehaviourEditor.cs:268)
UnityEditor.UIElements.<>c__DisplayClass74_0:b__0()
UnityEngine.UIElements.IMGUIContainer:DoOnGUI(Event, Matrix4x4, Rect, Boolean, Rect, Action, Boolean)
UnityEngine.UIElements.IMGUIContainer:DoMeasure(Single, MeasureMode, Single, MeasureMode)
UnityEngine.UIElements.VisualElement:Measure(VisualElement, LayoutNode&, Single, LayoutMeasureMode, Single, LayoutMeasureMode, LayoutSize&)
UnityEngine.UIElements.Layout.LayoutDelegates:InvokeMeasureFunction(LayoutNode&, Single, LayoutMeasureMode, Single, LayoutMeasureMode, LayoutSize&)
UnityEngine.UIElements.Layout.LayoutNative:CalculateLayout(IntPtr, Single, Single, Int32, IntPtr)
UnityEngine.UIElements.Layout.LayoutProcessorNative:UnityEngine.UIElements.Layout.ILayoutProcessor.CalculateLayout(LayoutNode, Single, Single, LayoutDirection)
UnityEngine.UIElements.Layout.LayoutProcessor:CalculateLayout(LayoutNode, Single, Single, LayoutDirection)
UnityEngine.UIElements.Layout.LayoutNode:CalculateLayout(Single, Single)
UnityEngine.UIElements.UIRLayoutUpdater:Update()
UnityEngine.UIElements.VisualTreeUpdater:UpdateVisualTreePhase(VisualTreeUpdatePhase)
UnityEngine.UIElements.Panel:UpdateWithoutRepaint()
UnityEditor.EditorElementUpdater:CreateInspectorElementsForMilliseconds(Int64)
UnityEditor.PropertyEditor:Update()
UnityEditor.HostView:SendUpdate()
UnityEditor.EditorApplication:Internal_CallUpdateFunctions()
Example usage of NetworkList:
The text was updated successfully, but these errors were encountered: