Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Got an InvalidOperationException for a simple code

Discussion in 'Entity Component System' started by davenirline, Nov 15, 2018.

  1. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    969
    I made this minimum test to make sure that I replicate the error with the smallest amount of code.

    Code (CSharp):
    1. public class TransformSystemBugTest : MonoBehaviour {
    2.         private EntityManager entityManager;
    3.  
    4.         private void Awake() {
    5.             this.entityManager = World.Active.GetOrCreateManager<EntityManager>();
    6.         }
    7.  
    8.         private void Update() {
    9.             if (Input.GetKey(KeyCode.Space)) {
    10.                 Entity entity = this.entityManager.CreateEntity();
    11.                 this.entityManager.AddComponentData(entity, new Position());
    12.                 this.entityManager.AddComponentData(entity, new Static());
    13.                 Debug.Log("Added dummy");
    14.             }
    15.         }
    16.     }
    When space is pressed, it throws this error:
    InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) <0x3622c370 + 0x00052> in <2f14d49edc704940aee557642343f344>:0
    Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckReadAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at C:/buildslave/unity/build/Runtime/Export/AtomicSafetyHandle.bindings.cs:143)
    Unity.Entities.ArchetypeChunk.GetNativeArray (Unity.Entities.ArchetypeChunkEntityType archetypeChunkEntityType) (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.17/Unity.Entities/Iterators/ArchetypeChunkArray.cs:63)
    Unity.Transforms.TransformSystem.UpdateFrozen () (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.17/Unity.Transforms/TransformSystem.cs:320)
    Unity.Transforms.TransformSystem.OnUpdate (Unity.Jobs.JobHandle inputDeps) (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.17/Unity.Transforms/TransformSystem.cs:1172)
    Unity.Entities.JobComponentSystem.InternalUpdate () (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.17/Unity.Entities/ComponentSystem.cs:507)
    Unity.Entities.ScriptBehaviourManager.Update () (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.17/Unity.Entities/ScriptBehaviourManager.cs:77)
    Unity.Entities.ScriptBehaviourUpdateOrder+DummyDelagateWrapper.TriggerUpdate () (at C:/Users/Marnel/AppData/Local/Unity/cache/packages/staging-packages.unity.com/com.unity.entities@0.0.12-preview.17/Unity.Entities/ScriptBehaviourUpdateOrder.cs:703)

    If I use Input.GeyKeyDown() or GetKeyUp(), there are no problems.
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    And it’s correct, you invaludate chunk used in TS, and with key up/down you can handle same error (but it not simple) if you up/down key at the very beginning of the frame but as I say above it’s not simple, capcure frame beginning :) may be execution order can help solve it...I’ll try to test tommorow
     
    Last edited: Nov 18, 2018
  3. 5argon

    5argon

    Joined:
    Jun 10, 2013
    Posts:
    1,555
    I believe this is a bug in TransformSystem where if in the same frame there is a thing to be "pending frozen" and "to freeze" at the same time it would throw..

    Frozen process is by 1. Adding Static 2. Wait for a frame to calculate one last transform matrix 3. Freeze it. The freeze step's ECB playback will invalidate if there is something to freeze, but there is no code to refresh the invalidated data. The freeze step is followed by pending frozen step. It will fail if it just freeze something prior. Just pending is fine + just freeze is fine but not both.

    When you use GetKey it make 2 consecutive frames that the routine runs. The first frame Static added with pending frozen, then 2nd frame you got a situation where one thing will get frozen and the other thing is entering pending frozen at the same time. (If you could GeyKeyDown in 2 consecutive frames you could trigger the same bug, or you could let it run on each Update and get the same bug.)

    For now just go in `TransformSystem.cs` in your package cache folder and look for the UpdatePendingFrozen() method body, and add GatherTypes() at the bottom most line. So if there are things to freeze it should refresh archetype chunks.
     
    davenirline and eizenhorn like this.
  4. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,753
    Any call to entity manager will force a sync.

    This is a bug with the TransformSystem and static.
    You can replicate it many different ways, using EntityCommandBuffers, EntityManager, using archetypes or just adding static component in same frame etc.

    -edit-

    beaten by 5argon, he has a much better explanation and a solution.
     
    eizenhorn likes this.
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    Yep agree, I check TS source and see :)
     
    Last edited: Nov 18, 2018