Search Unity

How to dispose NativeArray with ForEach lambda?

Discussion in 'Entity Component System' started by dips234, Oct 17, 2021.

  1. dips234

    dips234

    Joined:
    May 31, 2019
    Posts:
    9
    I'm getting "Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak". I thought adding the .WithDisposeOnCompletion(...) was enough. Anyone else ran into this issue yet?

    Code (CSharp):
    1.            
    2. NativeArray<D_Occupancy> otherOccupants = GetEntityQuery(ComponentType.ReadOnly<D_Occupancy>()).ToComponentDataArray<D_Occupancy>(Allocator.TempJob);
    3.             Entities
    4.                 .WithReadOnly(otherOccupants)
    5.                 .ForEach((Entity thisEntity, DynamicBuffer<B_Proximity> myProximityBuffer, in D_Occupancy myOccupantData) =>
    6.             {
    7.                 myProximityBuffer.Clear();
    8.                 int i = 0;
    9.                 while (i < otherOccupants.Length)
    10.                 {
    11.                     if (!(otherOccupants[i].entityId == thisEntity))
    12.                     {
    13.                         if (otherOccupants[i].QuadrantKey == myOccupantData.QuadrantKey)
    14.                         {
    15.                             myProximityBuffer.Add(otherOccupants[i].entityId);
    16.                         }
    17.                     }
    18.                     i++;
    19.                 }
    20.             })
    21.             .WithDisposeOnCompletion(otherOccupants)
    22.             .Schedule();
     
  2. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    After scheduling, try calling:
    Code (CSharp):
    1. otherOccupants.Dispose(this.Dependency);
     
  3. dips234

    dips234

    Joined:
    May 31, 2019
    Posts:
    9
    I still get the same warning. Shouldn't the .WithDisposeOnCompletion(otherOccupants) perform the allocation clearing?
     
  4. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    987
    It should but I'm not sure. We don't use Entities.ForEach() often. We use job structs. We call Dispose(JobHandle) after scheduling.
     
  5. dips234

    dips234

    Joined:
    May 31, 2019
    Posts:
    9
    Interesting development. I removed ".WithDisposeOnCompletion()" and "otherOccupants.Dispose(this.Dependency)".

    So far I haven't gotten the warning so feeling optimistic. I'll update this post if I do get the warning though. THANK YOU !!