Search Unity

DestroyEntity with group filter causes Exception in preview 23

Discussion in 'Entity Component System' started by GilCat, Jan 18, 2019.

  1. GilCat

    GilCat

    Joined:
    Sep 21, 2013
    Posts:
    676
    I just came across this new exception when switched to Entities preview 23.
    NullReferenceException: Object reference not set to an instance of an object
    Unity.Entities.EntityDataManager.RequiresBuildingResidueSharedComponentIndices (Unity.Entities.Archetype* srcArchetype, Unity.Entities.Archetype* dstArchetype) (at Library/PackageCache/com.unity.entities@0.0.12-preview.23/Unity.Entities/EntityDataManager.cs:1529)
    Unity.Entities.EntityDataManager.RemoveEntitiesInChunkWithSystemState (Unity.Entities.Chunk* chunk, Unity.Entities.ArchetypeManager archetypeManager, Unity.Entities.SharedComponentDataManager sharedComponentDataManager) (at Library/PackageCache/com.unity.entities@0.0.12-preview.23/Unity.Entities/EntityDataManager.cs:1133)
    Unity.Entities.EntityDataManager.DeleteChunks (Unity.Collections.NativeArray`1[T] chunks, Unity.Entities.EntityDataManager* manager, Unity.Entities.ArchetypeManager archetypeManager, Unity.Entities.SharedComponentDataManager sharedComponentDataManager) (at Library/PackageCache/com.unity.entities@0.0.12-preview.23/Unity.Entities/EntityDataManager.cs:1117)
    Unity.Entities.EntityManager.DestroyEntity (Unity.Entities.ComponentGroup componentGroupFilter) (at Library/PackageCache/com.unity.entities@0.0.12-preview.23/Unity.Entities/EntityManager.cs:391)
    TestSystem.OnUpdate () (at Assets/TestSystem.cs:15)
    Unity.Entities.ComponentSystem.InternalUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.23/Unity.Entities/ComponentSystem.cs:464)
    Unity.Entities.ScriptBehaviourManager.Update () (at Library/PackageCache/com.unity.entities@0.0.12-preview.23/Unity.Entities/ScriptBehaviourManager.cs:83)
    Unity.Entities.ScriptBehaviourUpdateOrder+DummyDelagateWrapper.TriggerUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.23/Unity.Entities/ScriptBehaviourUpdateOrder.cs:706)

    Here is a simple code that illustrates it.

    Code (CSharp):
    1. public struct TestComponent : IComponentData { }
    2.  
    3. public class TestSystem : ComponentSystem {
    4.   ComponentGroup m_group;
    5.  
    6.   protected override void OnCreateManager() {
    7.     base.OnCreateManager();
    8.     m_group = GetComponentGroup(typeof(TestComponent));
    9.   }
    10.  
    11.   protected override void OnUpdate() {
    12.     EntityManager.DestroyEntity(m_group);
    13.   }
    14. }
    15.  
    16. public class Bootstrap {
    17.   [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    18.   public static void InitializeAfterScene() {
    19.     var em = World.Active.GetOrCreateManager<EntityManager>();
    20.     em.CreateEntity(typeof(TestComponent));
    21.   }
    22. }
     
  2. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    I get the same though only after a few runs through.
    The code within DestroyEntity(group) has changed to use group.GetAllMatchingChunks() and DeleteChunks() instead of group.getEntityArray().

    The problem seems to be here
    Code (CSharp):
    1. Archetype* archetype = chunk->Archetype->SystemStateResidueArchetype;
    2. int numSharedComponents = archetype->NumSharedComponents;    // NullReferenceException here
    SystemStateResidueArchetype isn't null but it must refer to some invalid memory or something. I don't have a debugger so can't dig deeper.
    For now, I'm just deleting by the Entity array in each chunk.
    Code (CSharp):
    1. void destroyEntities(ComponentGroup group)
    2. {
    3.     var chunks = _group.CreateArchetypeChunkArray(Allocator.TempJob);
    4.     for (int i = 0; i < chunks.Length; i++)
    5.     {
    6.         EntityManager.DestroyEntity(chunks[i].GetNativeArray(entityChunkType));
    7.     }
    8.     chunks.Dispose();
    9. }
     
  3. capyvara

    capyvara

    Joined:
    Mar 11, 2010
    Posts:
    80
    It will be fixed on the next version, for now you can fix manually by initializing SystemStateResidueArchetype:

    At ArchetypeManager.cs near line 738
    Code (CSharp):
    1.             type->MetaChunkArchetype = null;
    2.             type->SystemStateResidueArchetype = null; // Add this
    3.             type->NumSharedComponents = 0;
    4.  
     
    Last edited: Jan 24, 2019
    Deleted User, eizenhorn and GilCat like this.
  4. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Thanks Capyvara, that works.