Search Unity

Can't dispose NativeArray

Discussion in 'Entity Component System' started by Spy-Shifty, Apr 9, 2019.

  1. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    I get the following error on executing:
    "InvalidOperationException: The NativeArray can not be Disposed because it was not allocated with a valid allocator."

    Code (CSharp):
    1.  
    2. protected override JobHandle OnUpdate(JobHandle inputDeps) {
    3.             _ProducerHandle.Complete();
    4.             _ProducerHandle = default;
    5.  
    6.             if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.S)) {
    7.                 _SpawnerRegistry.RegisterEntity(new NetworkIdentity());
    8.             }
    9.  
    10.             var networPrefabEntity = _NetworkPrefabQuery.GetSingletonEntity();
    11.             var networkEntityPrefabs = GetBufferFromEntity<NetworkEntityPrefabs>(true)[networPrefabEntity];
    12.  
    13.             using (var networkIdTypes = _SpawnerRegistry.GetNetworkIdentityTypes(Allocator.Temp)) {
    14.                 for (int i = 0; i < networkIdTypes.Length; i++) {
    15.                     using (var networkIdentities = _SpawnerRegistry.GetNetworkIdentitiesOfType(networkIdTypes[i], Allocator.Temp))
    16.                     using (var entities = new NativeArray<Entity>(networkIdentities.Length, Allocator.Temp)) {
    17.  
    18.                         EntityManager.Instantiate(networkEntityPrefabs[networkIdTypes[i]].Prefab, entities);
    19.                         var networkIdFromEntity = GetComponentDataFromEntity<NetworkIdentity>();
    20.                         for (int j = 0; j < entities.Length; j++) {
    21.                             networkIdFromEntity[entities[j]] = networkIdentities[j];
    22.                         }
    23.                     } // Error happens here
    24.                 }
    25.                 _SpawnerRegistry.Clear();
    26.             }
    27.  
    28.             return inputDeps;
    29.         }  
    30.  
    upload_2019-4-9_11-15-56.png

    Hints: Latest version of everything...
     
  2. Spy-Shifty

    Spy-Shifty

    Joined:
    May 5, 2011
    Posts:
    546
    Ahh sometimes...:oops::rolleyes:

    For anyone with an similar problem...

    _SpawnerRegistry.GetNetworkIdentitiesOfType(networkIdTypes, Allocator.Temp)
    returns a NativeArray from a NativeList...

    So the NativeArray can't be disposed. I have to return the original NativeList to dispose it.

    Code (CSharp):
    1. // I create a NativeList but I do return a NativeArray.
    2. //This isn't possible because you can't dispose it.
    3. internal NativeArray<NetworkIdentity> GetNetworkIdentitiesOfType(int type, Allocator allocator) {
    4.     var networkIdentities = new NativeList<NetworkIdentity>(allocator);
    5.     if (_Spawner.TryGetFirstValue(type, out NetworkIdentity item, out NativeMultiHashMapIterator<int> it)) {
    6.         do {
    7.             networkIdentities.Add(item);
    8.         } while (_Spawner.TryGetNextValue(out item, ref it));
    9.     }
    10.     return networkIdentities;
    11. }

    Anyway I think the feature to dispose an NativeArray from an NativeList would be nice to have..
    Usecase :
    • Disposing NativeArray from NativeLists in Jobs
    • Hide the ability to add/remove items to a collection and and dispose of them after usage.
     
    Last edited: Apr 9, 2019
    LuisEGV and Sarkahn like this.
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    It is not possible to dispose a NativeArray that is a AsNativeArray() of the NativeList.
    The NativeArray does not have access to all the lists data. The List still has a header.

    You need to dispose the NativeList instead, which implicitly deletes the AsNativeArray as well of course.
     
    MNNoxMortem and Tony_Max like this.
  4. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    w... tf.