A Native Collection has not been disposed, resulting in a memory leak.

Hello there! We’re currently migrating out MLAPI client/server (parallel) project to Netcode and after I restart the game I get greeted by some of these errors. I assume these are related to NetworkLists but I’m not sure what to look for. Does anyone has a clue how to properly dispose these?

A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeList`1:.ctor(Int32, AllocatorHandle) (at Library\PackageCache\com.unity.collections@1.0.0-pre.6\Unity.Collections\NativeList.cs:120)
Unity.Netcode.NetworkList`1:.ctor() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\NetworkVariable\Collections\NetworkList.cs:13)
System.Reflection.MonoCMethod:InternalInvoke(Object, Object[], Exception&)
System.Reflection.MonoCMethod:InternalInvoke(Object, Object[])
System.RuntimeType:CreateInstanceMono(Boolean)
System.RuntimeType:CreateInstanceSlow(Boolean, Boolean, Boolean, StackCrawlMark&)
System.RuntimeType:CreateInstanceDefaultCtor(Boolean, Boolean, Boolean, StackCrawlMark&)
System.Activator:CreateInstance(Type, Boolean)
Unity.Netcode.NetworkBehaviour:InitializeVariables() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkBehaviour.cs:425)
Unity.Netcode.NetworkObject:SetNetworkVariableData(FastBufferReader) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkObject.cs:861)
Unity.Netcode.NetworkSpawnManager:SpawnNetworkObjectLocally(NetworkObject, SceneObject&, FastBufferReader, Boolean) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Spawning\NetworkSpawnManager.cs:408)
Unity.Netcode.NetworkObject:AddSceneObject(SceneObject&, FastBufferReader, NetworkManager) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkObject.cs:1125)
Unity.Netcode.ConnectionApprovedMessage:Handle(FastBufferReader, UInt64, NetworkManager) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Messaging\Messages\ConnectionApprovedMessage.cs:84)
Unity.Netcode.ConnectionApprovedMessage:Receive(FastBufferReader, NetworkContext&) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Messaging\Messages\ConnectionApprovedMessage.cs:59)
Unity.Netcode.MessagingSystem:HandleMessage(MessageHeader&, FastBufferReader, UInt64, Single) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Messaging\MessagingSystem.cs:241)
Unity.Netcode.MessagingSystem:ProcessIncomingMessageQueue() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Messaging\MessagingSystem.cs:260)
Unity.Netcode.NetworkManager:OnNetworkEarlyUpdate() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkManager.cs:1154)
Unity.Netcode.NetworkManager:NetworkUpdate(NetworkUpdateStage) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkManager.cs:1125)
Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage(NetworkUpdateStage) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkUpdateLoop.cs:149)
Unity.Netcode.<>c:<CreateLoopSystem>b__0_0() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkUpdateLoop.cs:172)

If you know a simple way to reproduce this could you report a bug here?

Unfortunately I don’t know an easy way of reproduction, because I still don’t know what’s causing it. But in case we find it I’ll whether post the solution here or report it properly as you suggested.

I’m getting something similar by simply having

private NetworkList<int> _unavailableColors = new NetworkList<int>();

in a NetworkBehaviour.

A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
Unity.Collections.NativeList`1:.ctor(Int32, AllocatorHandle) (at Library\PackageCache\com.unity.collections@1.0.0-pre.5\Unity.Collections\NativeList.cs:120)
Unity.Netcode.NetworkList`1:.ctor() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\NetworkVariable\Collections\NetworkList.cs:13)
Shared.UI.LobbyPlayerColors:.ctor() (at Assets\Scripts\Shared\UI\Lobby\LobbyPlayerColors.cs:37)

This error only shows up when i do “Build & Run” or “Build”

The workaround is, don’t instantiate the NetworkList yourself in the code, the NetworkBehavior will do it on its own at runtime.

1 Like

Not initializing sounded good, but it didn’t solve the memory leaks for me.
What got rid of the errors for my spaghetti code was calling theList.Dispose() inside of the OnDestroy() method.
I’m not sure what I’m doing differently elsewhere. I don’t see any Dispose() calls in the Boss Room example, so I guess that code is cleaning up after itself in a different way.

3 Likes

I just came across a little more clarity. I was overriding NetworkBehaviour OnDestroy. There a github issue out there talking about overriding OnDestroy doesn’t call the base class’s implementation. So, for me, I can either stop overriding OnDestroy, or I can call base.OnDestroy(). Either one cleaned up the issue for me.

4 Likes

Deleting this snippet worked for me: = new NetworkList<int>()

1 Like

@ninjaguy369 , that’s expected, NetworkList can’t be initialized at declaration time (see the example here)

4 Likes

This did it for me! Moved the initialization to Start() and all the errors went away. Thanks!

Quick note: since OnNetworkSpawn() can be called before Start() (i.e. .on in scene placed network objects), I suggest always initializing NetworkLists in Awake() rather than Start().

I haven’t tested the specific case, so I’m not so sure if it would actually break things, but just in case :slight_smile:

1 Like

Hopefully not necroposting but this might help some other poor souls, what worked for me was:

  1. Avoiding the manual initialization on declaration
  2. Subscribing (and unsubbing) to .OnListChanged in OnNetworkSpawn()/OnNetworkDespawn() events