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.
Description
If a
NetworkBehaviourhas a protected or publicNetworkListin a class A, and you inherit from class A, then when you despawn the object, you get an exception:Reproduce Steps
Create classes:
And add
MyChildNetworkBehaviourto aNetworkObject.Spawn and Despawn the
NetworkObjectThe problem stems from
NetworkBehaviour.GetFieldInfoForTypeRecursivecallingGetFieldsat each level of inheritance hierarchy without usingBindingFlags.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
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.