I have found that when using netcode 0.6 and 2020.3.16f1 I am now seeing errors in the PhysicsWorldHistory.
Here is the error message code:
Error
ObjectDisposedException: Cannot access a disposed object.
Object name: ‘The NativeArray has been disposed, it is not allowed to access it’.
Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckExistsAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle& handle) (at :0)
Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.ValidateNonDefaultHandle (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle& handle) (at :0)
Unity.Collections.NativeArray`1[T].get_Length () (at :0)
Unity.Physics.Broadphase+Tree.get_NumBodies () (at Library/PackageCache/com.unity.physics@0.6.0-preview.3/Unity.Physics/Collision/World/Broadphase.cs:371)
Unity.Physics.Broadphase.get_NumStaticBodies () (at Library/PackageCache/com.unity.physics@0.6.0-preview.3/Unity.Physics/Collision/World/Broadphase.cs:28)
Unity.Physics.CollisionWorld.get_NumBodies () (at Library/PackageCache/com.unity.physics@0.6.0-preview.3/Unity.Physics/Collision/World/CollisionWorld.cs:23)
Unity.NetCode.CollisionHistoryBuffer.CloneCollisionWorld (System.Int32 index, Unity.Physics.CollisionWorld& collWorld) (at Packages/com.unity.netcode@0.6.0-preview.7/Runtime/Physics/PhysicsWorldHistory.cs:192)
Unity.NetCode.PhysicsWorldHistory.OnUpdate () (at Packages/com.unity.netcode@0.6.0-preview.7/Runtime/Physics/PhysicsWorldHistory.cs:349)
Unity.Entities.SystemBase.Update () (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/SystemBase.cs:412)
Unity.Entities.ComponentSystemGroup.UpdateAllSystems () (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystemGroup.cs:472)
UnityEngine.Debug:LogException(Exception)
Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/Stubs/Unity/Debug.cs:19)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystemGroup.cs:477)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystemGroup.cs:417)
Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystem.cs:114)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystemGroup.cs:472)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystemGroup.cs:417)
Unity.NetCode.ServerSimulationSystemGroup:OnUpdate() (at Packages/com.unity.netcode@0.6.0-preview.7/Runtime/ClientServerWorld/ServerSimulationSystemGroup.cs:75)
Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystem.cs:114)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystemGroup.cs:472)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystemGroup.cs:417)
Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystem.cs:114)
Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystemGroup.cs:472)
Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystemGroup.cs:417)
Unity.Entities.ComponentSystem:Update() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ComponentSystem.cs:114)
Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/ScriptBehaviourUpdateOrder.cs:333)
I have found this issue in multiple projects using netcode including the Unity mulitplayer sampleproject. This problem does not occur in 2020.3.15f2. My guess of the cause of this problem is the change in 2020.3.16f1 release notes:
The problematic code in this case is in CollisionHistoryBuffer’s CloneCollisionWorld method:
if (m_buffer.GetWorldAt(index).NumBodies > 0)
{
UnityEngine.Debug.LogError("Not disposing CollisionWorld before assign a new one might cause memory leak");
}
It looks like the issue stems from checking the length of a native array. Since this is a sanity check I found that commenting it out “fixes” the issue. Having the physics team add a IsCreated method or property would probably be a better long term solution (assuming this hasn’t been fixed in a later release already).