2020.3.16f1: Physics World History ObjectDisposedException: Cannot access a disposed object.

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).

5 Likes

Oh I was wondering why this was happening in a project I was just setting up.
Didn’t even click it was related to Unity 2020.3.16f

Side note: I also noticed at work the Unity compatible version of the Collections package wouldn’t compile for us due to NativeList.AsReadOnly failing without collection checks on

1 Like

I’m having the exact same issue

1 Like

I was able to get around this particular case as I described above. I have not had the problem @tertle mentioned, but I did have another weird issue where I could not build my project with IL2CPP and mono builds would fail at runtime if I had any Dynamic Buffer types that used the generate authoring component type. Everything worked in the editor; it was only a issue when building. Even more strange is that when I simply wrote authoring components for these type and removed the attribute everything (in builds with either IL2CPP or Mono and in the editor) was working again.

Thanks for reporting the issue. Looks like something changed in the 2020.3.16 that break that sanity check. We will definitively looks at that and properly fix it.

2 Likes

@CMarastoni Engine bug?

Not sure, I need to check what has changed. It is a behaviour I never seen before

@CMarastoni Will it fix at next Unity 2020.3.17 LTS?

Don’t have any update on that yet.

1 Like

FYI, I just chcked by updating to 2020.3.17f1, still happens

2 Likes

I’m running 2020.3.19f1 with netcode 0.6 and I’ve still got the problem.

Please just remove the check

#if ENABLE_UNITY_COLLECTIONS_CHECKS
            if (m_buffer.GetWorldAt(index).NumBodies > 0)
            {
                UnityEngine.Debug.LogError("Not disposing CollisionWorld before assign a new one might cause memory leak");
            }
#endif

The NumBodies API cannot be safely used anymore. We already have removed it.

2 Likes

Sorry for being a dummy but where can I find this file?

1 Like

It’s in \Library\PackageCache\com.unity.netcode@0.6.0-preview.7\Runtime\Physics\PhysicsWorldHistory.cs

I think you can be forgiven for not being able to find that :slight_smile:

2 Likes

@CMarastoni Actually this changes break Entities package and other projects. Technically the behavior has been changed. Is there still any plan to fix this engine level bug? The bug starting from 2020.4.16 until now still no fix yet.

What does it break in the entities package outside of netcode (and the collections issue I posted above)?
We’ve been testing 2020.3.16 since it first came out without problems (as it had a required fix we had been waiting on for 6months) and just released our game a week ago on steam without any issues.

@tertle Actually not really breaking at real build but the error spamming at editor is really annoying. I ady locally fixed it though.

This is another dumb question, does this not get overwritten every time the package gets re-imported? (like if I did reimport all assets would I have to fix this again?)

1 Like

You can create a local copy of the package by copying the package from the package cache in the library folder into the packages folder (where your manifest is). You can then edit the package freely without it being overwritten. I’ve done this to patch both the netcode and entities package.

1 Like