Search Unity

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

Discussion in 'Netcode for GameObjects' started by hoesterey, Jan 25, 2022.

  1. hoesterey

    hoesterey

    Joined:
    Mar 19, 2010
    Posts:
    659
    Resolved: Network Variables have to be on Network Behaviors...didn't think about that ;)

    Getting a memory leak with Network Lists when spawned in a class that is not a monobehavior. Is this expected behavior?

    CallStack:
    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.1.0\Unity.Collections\NativeList.cs:123)
    Unity.Netcode.NetworkList`1:.ctor() (at Library\PackageCache\com.unity.netcode.gameobjects@1.0.0-pre.4\Runtime\NetworkVariable\Collections\NetworkList.cs:13)
    GBPlugin.Networking.NetworkedIntIntDictionary:.ctor() (at Assets\GoldboxPlugins\Networking\Scripts\Shared\NetworkedVariables\NetworkedIntIntDictionary.cs:13)
    Game.AttributesBehavior:Awake() (at Assets\Game\Scripts\Shared\Gameplay\Attributes\AttributesBehavior.cs:23)


    Code:
    Code (CSharp):
    1. public class AttributesBehavior : NetworkBehaviour
    2. {
    3.            m_Attributes = new NetworkedIntIntDictionary();
    4.             m_AttributeBindMin = new NetworkedIntIntDictionary();
    5.             m_AttributeBindMax = new NetworkedIntIntDictionary();
    6.             m_Attributes.SetErrorLevel(true);
    7. }
    8.  
    9. public class NetworkedIntIntDictionary
    10.     {
    11.         public delegate void ChangedPropertyValue(NetworkedIntIntDictionary dictonary);
    12.  
    13.         NetworkList<int> m_KeyList = new NetworkList<int>();
    14.         NetworkList<int> m_ValueList = new NetworkList<int>();
    15.  
    16.         private bool m_AssertOnGetKeyMissing = false;
    17.      
    18.  
    19.         //bug in unity this works around as Contains key is inversed.
    20.         public bool OVERRIDECONTAINSKEY(int key)
    21.         {
    22.             return !m_KeyList.Contains(key);
    23.         }
    24.  
    25.         /// <summary>
    26.         /// set before use to change error level.
    27.         /// </summary>
    28.         /// <param name="assertOnGetKeyMissing">set true if you want to assert when a key you are trying to get does not exist.</param>
    29.         public void SetErrorLevel(bool assertOnGetKeyMissing = false)
    30.         {
    31.             m_AssertOnGetKeyMissing = assertOnGetKeyMissing;
    32.         }
    33. }
     
    Last edited: Jan 25, 2022