Skip to content

nonprivate NetworkList throws exception if class inherited #2335

@MikeSchurman

Description

@MikeSchurman

Description

If a NetworkBehaviour has a protected or public NetworkList in a class A, and you inherit from class A, then when you despawn the object, you get an exception:

ObjectDisposedException: The Unity.Collections.NativeList`1[System.Single] has been deallocated, it is not allowed to access it

Reproduce Steps

Create classes:

class MyBaseNetworkBehaviour : NetworkBehaviour
{
        protected override void Awake()
        {
            base.Awake();
            m_networkList = new NetworkList<float>();
        }
        protected NetworkList<float> m_networkList;
}

class MyChildNetworkBehaviour : MyBaseNetworkBehaviour 
{
}

And add MyChildNetworkBehaviour to a NetworkObject.
Spawn and Despawn the NetworkObject

The problem stems from NetworkBehaviour.GetFieldInfoForTypeRecursive calling GetFields at each level of inheritance hierarchy without using BindingFlags.DeclaredOnly, which causes nonpublic netvars to get added more than once.

Actual Outcome

m_networkList is disposed of twice and throws exception 2nd time.

Expected Outcome

No exception.

Screenshots

If applicable, add screenshots to help explain your problem.

Environment

  • OS: Windows 10
  • Unity Version: 2021.36f1
  • Netcode Version: 1.2.0
  • Netcode Commit:

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.

Metadata

Metadata

Assignees

Labels

priority:mediumThis issue has medium priority and may take some time to be resolvedstat:importedStatus - Issue is tracked internally at Unitytype:bugBug Report

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions