Search Unity

[SOLVED] WithDeallocateOnJobCompletion with multiple ForEach

Discussion in 'Entity Component System' started by floboc, Mar 21, 2020.

  1. floboc

    floboc

    Joined:
    Oct 31, 2017
    Posts:
    91
    Hello there :)
    I have a system running two jobs in parallel that use the same chunk array. Something like so:

    Code (CSharp):
    1. protected override void OnUpdate()
    2. {
    3.     // ... some logic ...
    4.  
    5.     // get chunk array
    6.     var impact_chunks = impact_group.CreateArchetypeChunkArrayAsync(Allocator.TempJob, out var impact_handle);
    7.     Dependency = JobHandle.CombineDependencies(Dependency, impact_handle);
    8.  
    9.     // schedule first job
    10.     var handle1 =
    11.     Entities
    12.     .WithReadOnly(impact_chunks)
    13.     .ForEach(( /* ... */ ) =>
    14.     {
    15.         // ... some logic here ...
    16.     })
    17.     .ScheduleParallel(Dependency);
    18.  
    19.     // schedule second job
    20.     var handle2 =
    21.     Entities
    22.     .WithReadOnly(impact_chunks)
    23.     .ForEach(( /* ... */ ) =>
    24.     {
    25.         // ... some logic here ...
    26.     })
    27.     .ScheduleParallel(Dependency);
    28.  
    29.     Dependency = JobHandle.CombineDependencies(handle1, handle2);
    30. }
    How can I make sure that my chunk array is disposed after both jobs are completed ?
    - I cannot use WithDeallocateOnJobCompletion on the first job
    - I cannot use WithDeallocateOnJobCompletion on the second job without forcing it to run after the first one (not ideal)
    - If I want to use WithDeallocateOnJobCompletion on a third job depending on these two jobs, this new job must use the chunk array in its logic, otherwise this throws an error 5local variable not captured as it is not used)

    What is the official recommended solution to this case?
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,269
    Code (CSharp):
    1. Dependency = impact_chunks.Dispose(Dependency);
     
  3. floboc

    floboc

    Joined:
    Oct 31, 2017
    Posts:
    91
    Amazing thanks! I don't know how I missed that in the doc :)