Use NativeList / NativeArray in two jobs without Complete()

Is this possible?

protected override void OnUpdate()
        {
            var deltaDatas = new NativeList<DamageableDeltaData>(0, Allocator.TempJob);

            var entJob = Entities
                .ForEach((ref DynamicBuffer<EntityEventBuffer> buffer, in EntityEventDamageableDelta dam) =>
                {
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        deltaDatas.Add(new DamageableDeltaData { entity = buffer[i].affected, dam = dam });
                    }
                })
                .Schedule(Dependency);

            var addJob = new AddDamageableDeltaJob
            {
                data = deltaDatas,
                buffer = GetBufferFromEntity<DamageableDeltaBuffer>()
            }
            .Schedule(entJob);

            deltaDatas.Dispose();
        }

Change deltaDatas.Dispose(); to deltaDatas.Dispose(addJob); and you are all set.

1 Like

Beauty thanks! I also had to update my dependency. new code:

protected override void OnUpdate()
        {
            var deltaDatas = new NativeList<DamageableDeltaData>(0, Allocator.TempJob);

            var entJob = Entities
                .ForEach((ref DynamicBuffer<EntityEventBuffer> buffer, in EntityEventDamageableDelta dam) =>
                {
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        deltaDatas.Add(new DamageableDeltaData { entity = buffer[i].affected, dam = dam });
                    }
                })
                .Schedule(Dependency);

            Dependency = new AddDamageableDeltaJob
            {
                data = deltaDatas,
                buffer = GetBufferFromEntity<DamageableDeltaBuffer>()
            }
            .Schedule(entJob);

            deltaDatas.Dispose(Dependency);
        }

Dispose with a JobHandle returns the dependency, so you can do:

Dependency = deltaDatas.Dispose(addJob);

I don’t think it matters here, but there are situations where this can cause problems because the next Dependency is not aware of the array.

1 Like

Is there? Because we never do it. If you know of a specific case be great to share.

I think that thought stemmed from my post here, where the arrays gave a memory leak warning when when I didn’t use the given handles, despite that I was calling .Complete() on the final:

Granted that is using jobs outside of SystemBase. I can’t immediately think of a specific case for SystemBase where this would certainly happen, I just do it automatically now juuuust in case.