Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

Discussion in 'NetCode for ECS' started by desertGhost_, Aug 19, 2021.

  1. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    261
    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:
    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 <a0ef933b1aa54b668801ea864e4204fe>:0)
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.ValidateNonDefaultHandle (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle& handle) (at <a0ef933b1aa54b668801ea864e4204fe>:0)
    Unity.Collections.NativeArray`1[T].get_Length () (at <a0ef933b1aa54b668801ea864e4204fe>: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:
    Code (CSharp):
    1.  
    2.             if (m_buffer.GetWorldAt(index).NumBodies > 0)
    3.             {
    4.                 UnityEngine.Debug.LogError("Not disposing CollisionWorld before assign a new one might cause memory leak");
    5.             }
    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).
     
    CyFy, PhilSA, reeseschultz and 2 others like this.
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,775
    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
     
    adammpolak likes this.
  3. TRS6123

    TRS6123

    Joined:
    May 16, 2015
    Posts:
    246
    I'm having the exact same issue
     
    adammpolak likes this.
  4. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    261
    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.
     
  5. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    941
    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.
     
    adammpolak and optimise like this.
  6. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,133
    @CMarastoni Engine bug?
     
  7. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    941
    Not sure, I need to check what has changed. It is a behaviour I never seen before
     
  8. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,133
    @CMarastoni Will it fix at next Unity 2020.3.17 LTS?
     
  9. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    941
    Don't have any update on that yet.
     
    adammpolak likes this.
  10. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    FYI, I just chcked by updating to 2020.3.17f1, still happens
     
    Occuros and sleep2death like this.
  11. BlipB

    BlipB

    Joined:
    May 2, 2019
    Posts:
    2
    I'm running 2020.3.19f1 with netcode 0.6 and I've still got the problem.
     
  12. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    941
    Please just remove the check
    Code (csharp):
    1.  
    2. #if ENABLE_UNITY_COLLECTIONS_CHECKS
    3.             if (m_buffer.GetWorldAt(index).NumBodies > 0)
    4.             {
    5.                 UnityEngine.Debug.LogError("Not disposing CollisionWorld before assign a new one might cause memory leak");
    6.             }
    7. #endif
    8.  
    The NumBodies API cannot be safely used anymore. We already have removed it.
     
    l33t_P4j33t and LTK like this.
  13. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    Sorry for being a dummy but where can I find this file?
     
    Krooq likes this.
  14. MrEastwood

    MrEastwood

    Joined:
    Dec 9, 2013
    Posts:
    19
    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 :)
     
    LTK and adammpolak like this.
  15. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,133
    @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.
     
  16. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,775
    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.
     
  17. optimise

    optimise

    Joined:
    Jan 22, 2014
    Posts:
    2,133
    @tertle Actually not really breaking at real build but the error spamming at editor is really annoying. I ady locally fixed it though.
     
  18. adammpolak

    adammpolak

    Joined:
    Sep 9, 2018
    Posts:
    450
    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?)
     
    Krooq likes this.
  19. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    261
    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.
     
    adammpolak likes this.