but it’s not compatible with the last version of netcode.
I’ve fixed it up to be compatible with 1.8.1 (just changed some properties and it worked):
But when i use it (in a NetBehaviour), unity complains that there’s a memory leak:
“A Native Collection has not been disposed, resulting in a memory leak” x4, pointing to these 4 properties
private NativeList<TKey> m_Keys = new NativeList<TKey>(64, Allocator.Persistent);
private NativeList<TValue> m_Values = new NativeList<TValue>(64, Allocator.Persistent);
private NativeList<TKey> m_KeysAtLastReset = new NativeList<TKey>(64, Allocator.Persistent);
private NativeList<TValue> m_ValuesAtLastReset = new NativeList<TValue>(64, Allocator.Persistent);
It LOOKS like they’re being disposed in the .Dispose() function… Is there some other way that this custom network variable needs to be diposed in 1.8.1 ??
Do these even need to be native lists? Why can’t they just be regular managed Lists ?
Here’s a super simple, cut down example that shows the memory leak… shouldn’t this be fine??
public class ExampleNetworkVariable<TKey> :
NetworkVariableBase
where TKey : unmanaged
{
private NativeList<TKey> m_Keys = new NativeList<TKey>(64, Allocator.Persistent);
public override void Dispose()
{
m_Keys.Dispose();
base.Dispose();
}
public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
{
}
public override void ReadField(FastBufferReader reader)
{
}
public override void WriteDelta(FastBufferWriter writer)
{
}
public override void WriteField(FastBufferWriter writer)
{
}
}
if you use it in your code anywhere, the editor shows this error:
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.2.4\Unity.Collections\NativeList.cs:116)
It looks like NGO will support NetworkVariable dictionaries soon according to this . The PR is already on the develop branch, I had a quick play and the dictionary appears to sync but I don’t know how it picks up changes to the dictionary.
actually i found the issue. I was initializing the dictionaries in the class like so
public sealed class SampleClass : NetworkBehaviour
{
...
public NetworkDictionary<ulong, int> NetworkDictionary = new NetworkDictionary<ulong, int>();
...
}
changing it to this seemed to get rid of the memory leak warnings:
public sealed class SampleClass : NetworkBehaviour
{
...
public NetworkDictionary<ulong, int> NetworkDictionary;
private void Awake()
{
NetworkDictionary = new NetworkDictionary<ulong, int>();
}
...
}
But also, good to hear. i think an official network dictionary would be handy
Hmm. The updated code doesnt seem to work. The dictionary does not appear to synchronize itself across the clients (only on the server). I suspect its not triggering because i had to change this code from: