Can't dispose native container even after calling jobHandle.Complete()

Hi! I get this error in one of my ISystem when trying to dispose a native container used in jobs. This container is [ReadOnly] in the jobs.

InvalidOperationException: The previously scheduled job SGatheringEffectAtt:JCheckTenant_Podium reads from the Unity.Collections.NativeList`1[ZhCollection.Tuple`2[SectionArchitect.TenantInfo,Unity.Mathematics.int2]] JCheckTenant_Podium.infos. You must call JobHandle.Complete() on the job SGatheringEffectAtt:JCheckTenant_Podium, before you can deallocate the Unity.Collections.NativeList`1[ZhCollection.Tuple`2[SectionArchitect.TenantInfo,Unity.Mathematics.int2]] safely.

But I have already completed the job. See line 20.

This is the method where the error occurs at the line 23.

void CheckPodium(Entity building, ref SystemState state)
{
    var floors = SystemAPI.GetBuffer<PodiumFloors>(building);
    var podiumInfo = SystemAPI.GetComponentRO<PodiumInfo>(building);

    NativeList<Tuple<TenantInfo, int2>> infos = new(20, Allocator.TempJob);
    NativeArray2D<int> map = new(podiumInfo.ValueRO.Width() / Const.GridSize, floors.Length - 1, Allocator.TempJob);


    _buildPodiumInfo(ref infos, ref map, floors, podiumInfo.ValueRO.LeftX(), ref state);

    JobHandle jobHandle = new();

    for (int i = 0; i < infos.Length; i++)
    {
        if (_checkTenant_Podium(i, out var jh, ref infos, ref map, ref state))
            JobHandle.CombineDependencies(jobHandle, jh);
    }

    jobHandle.Complete();


    infos.Dispose();
    map.Dispose();
}

This is the _checkTenant_Podium method which schedules a job and returns its handle.

bool _checkTenant_Podium(int index, out JobHandle jobHandle, ref NativeList<Tuple<TenantInfo, int2>> infos, ref NativeArray2D<int> map, ref SystemState state)
{
    jobHandle = default;

    // if is garden return
    if (infos[index].Item1.type == TenantType.Type.Garden) return false;

    jobHandle = new JCheckTenant_Podium
    {
        infos = infos,
        map = map,
        index = index,
        demand = SystemAPI.GetComponent<TenantTypeID>(infos[index].Item1.entity).Type.attDemand,
        att = SystemAPI.GetComponentRW<TenantAtt>(infos[index].Item1.entity)
    }.Schedule();

    return true;
}

Can anyone help with my problem?

Line 17 in the first snippet does not assign back to the local variable jobhandle - you’re completing nothing with that final call on line 20 because of this. You need to assign the returned value from CompleteDependencies to jobhandle to correctly chain jobs.

1 Like

Oh! I see. Thanks!