Search Unity

Is it safe that using NativeArray for JobHandle.CombineDependencies() without Dispose()?

Discussion in 'Entity Component System' started by Kichang-Kim, May 10, 2019.

  1. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,011
    Hi, I found that some odd code in Unity Multiplayer sample code (https://github.com/Unity-Technologies/multiplayer)

    The code is located at RenderInterpolationSystem.cs, line 211:
    Code (CSharp):
    1. protected override JobHandle OnUpdate(JobHandle inputDeps)
    2. {
    3.     RenderInterpolationSystem.parameters.startTime = Time.fixedTime;
    4.     RenderInterpolationSystem.parameters.fixedDeltaTime = Time.fixedDeltaTime;
    5.  
    6.     var posJob = new UpdatePos();
    7.     posJob.positionType = GetArchetypeChunkComponentType<Translation>();
    8.     posJob.curPositionType = GetArchetypeChunkComponentType<CurrentSimulatedPosition>(true);
    9.     posJob.prevPositionType = GetArchetypeChunkComponentType<PreviousSimulatedPosition>();
    10.     posJob.simStartComponentVersion = simStartComponentVersion;
    11.     posJob.simEndComponentVersion = simEndComponentVersion;
    12.  
    13.     var rotJob = new UpdateRot();
    14.     rotJob.rotationType = GetArchetypeChunkComponentType<Rotation>();
    15.     rotJob.curRotationType = GetArchetypeChunkComponentType<CurrentSimulatedRotation>(true);
    16.     rotJob.prevRotationType = GetArchetypeChunkComponentType<PreviousSimulatedRotation>();
    17.     rotJob.simStartComponentVersion = simStartComponentVersion;
    18.     rotJob.simEndComponentVersion = simEndComponentVersion;
    19.  
    20.     var handles = new NativeArray<JobHandle>(2, Allocator.Temp);
    21.     handles[0] = posJob.Schedule(positionInterpolationGroup, inputDeps);
    22.     handles[1] = rotJob.Schedule(rotationInterpolationGroup, inputDeps);
    23.  
    24.     simStartComponentVersion = GlobalSystemVersion;
    25.  
    26.     return JobHandle.CombineDependencies(handles);
    27. }
    You can find that "var handles = new NativeArray<JobHandle>(2, Allocator.Temp)" is used for JobHandle.CombineDependencies(), but there is no Dispose() for it, also there is no runtime warning when I play this sample. I want to know this code is correct or bug.

    Thanks.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    I believe Allocator.Temp is automatically disposed at end of frame (as of 19.1) which is why you can use it in jobs.

    I don't recall seeing official classification of what is happening with it now though.
     
    Kichang-Kim likes this.
  3. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,011
    I found this in 2018.3 release note:

    So the issue is the side effect of this changes?