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?
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.
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.
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.
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.
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