Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Issue report: Fixed Array in entity are disposed when destroying other entities

Discussion in 'Entity Component System' started by yc960, Jun 19, 2018.

  1. yc960

    yc960

    Joined:
    Apr 30, 2015
    Posts:
    228
    the error "InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it" will be throw when running the following code and calling Test() function.
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Collections;
    3. using Unity.Entities;
    4.  
    5. public class FixedArrayTest : MonoBehaviour
    6. {
    7.     NativeArray<Entity> entities;
    8.  
    9.     EntityManager entityManager;
    10.  
    11.     struct EntityHolder
    12.     {
    13.         public Entity En;
    14.     }
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         entityManager = World.Active.GetOrCreateManager<EntityManager>();
    19.  
    20.         entities = new NativeArray<Entity>(100, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
    21.         entityManager.Instantiate(entityManager.CreateEntity(ComponentType.FixedArray(typeof(EntityHolder), 2)), entities);
    22.  
    23.  
    24.         var array0 = new NativeArray<Entity>(100, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
    25.         entityManager.Instantiate(entityManager.CreateEntity(), array0);
    26.  
    27.         var array1 = new NativeArray<Entity>(100, Allocator.Temp, NativeArrayOptions.UninitializedMemory);
    28.         entityManager.Instantiate(entityManager.CreateEntity(), array1);
    29.  
    30.         for (int i = 0; i < entities.Length; i++)
    31.         {
    32.             var fixedArray = entityManager.GetFixedArray<EntityHolder>(entities[i]);
    33.             fixedArray[0] = new EntityHolder { En = array0[i] };
    34.             fixedArray[1] = new EntityHolder { En = array1[i] };
    35.         }
    36.         array0.Dispose();
    37.         array1.Dispose();
    38.     }
    39.  
    40.    
    41.     void Test()
    42.     {
    43.         for (int i = 0; i < entities.Length; i++)
    44.         {
    45.             var array = entityManager.GetFixedArray<EntityHolder>(entities[i]);
    46.  
    47.             entityManager.DestroyEntity(array[0].En);
    48.             entityManager.DestroyEntity(array[1].En);
    49.  
    50.         }
    51.     }
    52. }
    with the line " entityManager.DestroyEntity(array[1].En);" being pointed as the source of the error.

    Do note that the entity hosting the array is not touched.

    also, if I have only "entityManager.DestroyEntity(array[0].En);" without accessing the second element in the array, it work without error.
     
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,350