com.unity.netcode.gameobjects icon indicating copy to clipboard operation
com.unity.netcode.gameobjects copied to clipboard

INetworkSerialization issues with NativeArrays and NativeLists.

Open MilkyChestnut opened this issue 1 year ago • 4 comments

Description

NativeArrays seem to throw a memory leak error if I attempt to serialize the class using them with INetworkSerializable. NetworkSerialize with a NativeArray and setting the Allacator.Persistent will throw this memory leak issue.

` public class TestActionSystem : INetworkSerializable, IDisposable {

    private NativeArray<int> _intArray;
    
    public void Setup()
    {
        _intArray = new NativeArray<int>(1, Allocator.Persistent);
    }
    

    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
       
        serializer.SerializeValue(ref _intArray, Allocator.Persistent); //this will throw the error
    }
    
    public void Dispose()
    {
        Debug.Log($"Disposing _intArray {_intArray.IsCreated} {_intArray.Length}");
        if (_intArray.IsCreated)
        {
            _intArray.Dispose();
        }
        
    }
}

` NativeArrays with a custom struct also break if that custom struct impliments INetworkSerializable. If i dont add that interface and or serialize using the methods here it does work.

NativeLists dont even appear to work. I tried the simplest version with just ints, and this didn't work . ` public class TestActionSystem : INetworkSerializable, IDisposable {

    public NativeList<int> IntList => _intList;
    
    private NativeList<int> _intList;
    
    public void Setup()
    {
        _intList = new NativeList<int>(0, Allocator.Persistent);
    }

    public void Add(int value)
    {
        _intList.Add(value);
    }


    public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    {
        serializer.SerializeValue(ref _intList);
    }

   

    public void Dispose()
    {
        if (_intList.IsCreated)
        {
            _intList.Dispose();
        }
    }
}

`

My goal is hopefully just use a NativeList with my own custom structs with a few more NativeLists inside (to get around the issue of directly nesting NativeLists with other NativeLists.

But I can't even get the NativeArrays to work without a memory leak, and the NativeLists to even serialize.

Reproduce Steps

Provided in description

Actual Outcome

NativeArray with a int fires a memory leak exception. (needs to work with custom structs)

NativeList with a int won't even serailize.

Expected Outcome

NativeArray doesn't fire memory leak

NativeList serailizes on the network.

Screenshots

If applicable, add screenshots to help explain your problem.

Environment

  • OS: Windows 11 Home 23H2
  • Unity Version: 6000.0.19f1
  • Netcode Version: 2.0.0 (latest release full version)

Additional Context

Add any other context about the problem here. Logs, code snippets would be useful here but please also consider attaching a minimal Unity project that reproduces the issue.

MilkyChestnut avatar Sep 19 '24 22:09 MilkyChestnut

Further follow up, conversion methods for NativeList don't seem to work either. I get an error about things not being implimented.

_intArray = _nativeIntList.ToArray();

MilkyChestnut avatar Sep 19 '24 22:09 MilkyChestnut

@MilkyChestnut If you could provide me with the scripts you are using for both the INetworkSerializable implementations and the scripts that use them it would be very helpful. As an example, I am not sure if you are using TestActionSystem in an RPC or as the type within a NetworkVariable. Knowing the context of usage will help me to narrow down this issue so I can replicate it on my side.

NoelStephensUnity avatar Sep 28 '24 18:09 NoelStephensUnity

@NoelStephensUnity Sure just give me some time to setup a demo. I've resorted to using work arounds fn with just regular arrays and lists that I serialized into arrays. I'm crunching rn so give me time to finish and Ill wip up a demo project for you.

And TestActionSystem is passed via a RPC

MilkyChestnut avatar Sep 30 '24 20:09 MilkyChestnut

I haven't yet had the chance to make a demo, but I can probably make one soon. I uncovered another issue with NetworkVariable<List<>>

It for some reason desyncs if I have the host disconnect.

It works the first time but after disconnecting, events wont work anymore.

MilkyChestnut avatar Jan 17 '25 03:01 MilkyChestnut

Just to follow up on this issue. We've fixed a few bugs in the NetworkVariables collections in the latest versions of NGO. Would you mind updating to the latest version (v2.5.0) and seeing if you can reproduce these issues?

EmandM avatar Aug 25 '25 15:08 EmandM

This issue has been automatically marked as stale because it has been awaiting response for over 30 days without any activity. Please update the issue with any new information or it may be closed in 30 days.

github-actions[bot] avatar Sep 25 '25 00:09 github-actions[bot]

This issue has been automatically closed because it has been stale for 30 days without any activity. Feel free to reopen if you have new information to add.

github-actions[bot] avatar Oct 25 '25 00:10 github-actions[bot]