I recently converted one of my jobs to use the ForEach style instead of the IJobForEachWithEntity struct style. After converting I started to see “A Native Collection has not been disposed” exceptions. Mostly, I suspect because previously I was able to mark the collections for disposal after the job completes with the DeallocateOnJobCompletion attribute. This is what my code looks like:
var laneExitBuffer = GetBufferFromEntity<LaneExitEntity>(true);
NativeArray<Unity.Mathematics.Random> RandomGenerator = new NativeArray<Unity.Mathematics.Random>(JobsUtility.MaxJobThreadCount, Allocator.TempJob, NativeArrayOptions.UninitializedMemory);
for (int i = 0; i < JobsUtility.MaxJobThreadCount; i++)
{
RandomGenerator[i] = new Unity.Mathematics.Random((uint)randomSource.NextInt());
}
var CommandBuffer = _entityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
var job = Entities.WithAll<NeedsNextLaneTag, ERAutoTag>()
.ForEach((Entity entity,
int entityInQueryIndex,
int nativeThreadIndex,
ref VehicleLaneComponent vehicleLaneComponent) =>
{
// ... snip ...
})
.WithReadOnly(laneExitBuffer)
.Schedule(inputDeps);
_entityCommandBufferSystem.AddJobHandleForProducer(job);
return job;
The offending collection is RandomGenerator. How do I properly dispose of that collection?