Search Unity

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

Discussion in 'Netcode for GameObjects' started by vikpek, Nov 17, 2021.

  1. vikpek

    vikpek

    Joined:
    Aug 17, 2014
    Posts:
    7
    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?

    Code (Log):
    1. A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
    2. Unity.Collections.NativeList`1:.ctor(Int32, AllocatorHandle) (at Library\PackageCache\com.unity.collections@1.0.0-pre.6\Unity.Collections\NativeList.cs:120)
    3. Unity.Netcode.NetworkList`1:.ctor() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\NetworkVariable\Collections\NetworkList.cs:13)
    4. System.Reflection.MonoCMethod:InternalInvoke(Object, Object[], Exception&)
    5. System.Reflection.MonoCMethod:InternalInvoke(Object, Object[])
    6. System.RuntimeType:CreateInstanceMono(Boolean)
    7. System.RuntimeType:CreateInstanceSlow(Boolean, Boolean, Boolean, StackCrawlMark&)
    8. System.RuntimeType:CreateInstanceDefaultCtor(Boolean, Boolean, Boolean, StackCrawlMark&)
    9. System.Activator:CreateInstance(Type, Boolean)
    10. Unity.Netcode.NetworkBehaviour:InitializeVariables() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkBehaviour.cs:425)
    11. Unity.Netcode.NetworkObject:SetNetworkVariableData(FastBufferReader) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkObject.cs:861)
    12. 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)
    13. Unity.Netcode.NetworkObject:AddSceneObject(SceneObject&, FastBufferReader, NetworkManager) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkObject.cs:1125)
    14. 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)
    15. Unity.Netcode.ConnectionApprovedMessage:Receive(FastBufferReader, NetworkContext&) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Messaging\Messages\ConnectionApprovedMessage.cs:59)
    16. 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)
    17. Unity.Netcode.MessagingSystem:ProcessIncomingMessageQueue() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Messaging\MessagingSystem.cs:260)
    18. Unity.Netcode.NetworkManager:OnNetworkEarlyUpdate() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkManager.cs:1154)
    19. Unity.Netcode.NetworkManager:NetworkUpdate(NetworkUpdateStage) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkManager.cs:1125)
    20. Unity.Netcode.NetworkUpdateLoop:RunNetworkUpdateStage(NetworkUpdateStage) (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkUpdateLoop.cs:149)
    21. Unity.Netcode.<>c:<CreateLoopSystem>b__0_0() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\Core\NetworkUpdateLoop.cs:172)
    22.  
    23.  
     
  2. luke-unity

    luke-unity

    Joined:
    Sep 30, 2020
    Posts:
    306
    If you know a simple way to reproduce this could you report a bug here?
     
  3. vikpek

    vikpek

    Joined:
    Aug 17, 2014
    Posts:
    7
    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.
     
  4. TheCaveOfWonders

    TheCaveOfWonders

    Joined:
    Mar 2, 2014
    Posts:
    27
    I'm getting something similar by simply having
    Code (CSharp):
    1. private NetworkList<int> _unavailableColors = new NetworkList<int>();
    in a NetworkBehaviour.

    Code (CSharp):
    1. A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
    2. Unity.Collections.NativeList`1:.ctor(Int32, AllocatorHandle) (at Library\PackageCache\com.unity.collections@1.0.0-pre.5\Unity.Collections\NativeList.cs:120)
    3. Unity.Netcode.NetworkList`1:.ctor() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.3\Runtime\NetworkVariable\Collections\NetworkList.cs:13)
    4. 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.
     
    Last edited: Nov 28, 2021
    luke-unity likes this.
  5. RSolids

    RSolids

    Joined:
    May 19, 2021
    Posts:
    22
    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.
     
  6. RSolids

    RSolids

    Joined:
    May 19, 2021
    Posts:
    22
    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.
     
  7. ninjaguy369

    ninjaguy369

    Joined:
    Mar 3, 2014
    Posts:
    2
    Deleting this snippet worked for me:
    Code (CSharp):
    1. = new NetworkList<int>()
     
  8. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    @ninjaguy369 , that's expected, NetworkList can't be initialized at declaration time (see the example here)
     
  9. dennyroberts

    dennyroberts

    Joined:
    Feb 14, 2016
    Posts:
    8
    This did it for me! Moved the initialization to Start() and all the errors went away. Thanks!
     
  10. neviovalsa

    neviovalsa

    Joined:
    Jun 24, 2019
    Posts:
    52
    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 :)