Can't dispose NativeArray

I get the following error on executing:
“InvalidOperationException: The NativeArray can not be Disposed because it was not allocated with a valid allocator.”

protected override JobHandle OnUpdate(JobHandle inputDeps) {
            _ProducerHandle.Complete();
            _ProducerHandle = default;

            if (UnityEngine.Input.GetKeyDown(UnityEngine.KeyCode.S)) {
                _SpawnerRegistry.RegisterEntity(new NetworkIdentity());
            }

            var networPrefabEntity = _NetworkPrefabQuery.GetSingletonEntity();
            var networkEntityPrefabs = GetBufferFromEntity<NetworkEntityPrefabs>(true)[networPrefabEntity];

            using (var networkIdTypes = _SpawnerRegistry.GetNetworkIdentityTypes(Allocator.Temp)) {
                for (int i = 0; i < networkIdTypes.Length; i++) {
                    using (var networkIdentities = _SpawnerRegistry.GetNetworkIdentitiesOfType(networkIdTypes[i], Allocator.Temp))
                    using (var entities = new NativeArray<Entity>(networkIdentities.Length, Allocator.Temp)) {

                        EntityManager.Instantiate(networkEntityPrefabs[networkIdTypes[i]].Prefab, entities);
                        var networkIdFromEntity = GetComponentDataFromEntity<NetworkIdentity>();
                        for (int j = 0; j < entities.Length; j++) {
                            networkIdFromEntity[entities[j]] = networkIdentities[j];
                        }
                    } // Error happens here
                }
                _SpawnerRegistry.Clear();
            }

            return inputDeps;
        }

Hints: Latest version of everything…

Ahh sometimes…:sweat_smile::roll_eyes:

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.

csharp* *// I create a NativeList but I do return a NativeArray. //This isn't possible because you can't dispose it. internal NativeArray<NetworkIdentity> GetNetworkIdentitiesOfType(int type, Allocator allocator) { var networkIdentities = new NativeList<NetworkIdentity>(allocator); if (_Spawner.TryGetFirstValue(type, out NetworkIdentity item, out NativeMultiHashMapIterator<int> it)) { do { networkIdentities.Add(item); } while (_Spawner.TryGetNextValue(out item, ref it)); } return networkIdentities; }* *

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.
2 Likes

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.

2 Likes

w… tf.