Search Unity

Resolved Editor NativeArray<> not being disposed of immediately

Discussion in 'Entity Component System' started by kanomeister, Jan 24, 2022.

  1. kanomeister

    kanomeister

    Joined:
    May 16, 2017
    Posts:
    2
    Hi all, I've dug through tons of threads here and there, but I couldn't seem to find the root of this!

    I have an IJobParallelFor like so:
    Code (CSharp):
    1.     [BurstCompile]
    2.     internal struct FindInBetweenJob : IJobParallelFor
    3.     {
    4.         [ReadOnly] [DeallocateOnJobCompletion] public NativeArray<Vector3> verts;
    5.         [WriteOnly] public NativeArray<FindInBetween_BadVertex> isBad;
    6.  
    7.         public void Execute(int i)
    8.         {...}
    9.     }
    where FindInBetween_BadVertex is a simple three-primitive struct, and I call into it like so -- all of this intending to occur during the same frame during a synchronous function call on an object:

    Code (CSharp):
    1.     var isBad = new NativeArray<FindInBetween_BadVertex>(verts.Length, Allocator.TempJob);
    2.     var job = new FindInBetweenJob
    3.     {
    4.         isBad = isBad, verts = new NativeArray<Vector3>(verts, Allocator.TempJob)
    5.     };
    6.  
    7.     var jobHandle = job.Schedule(verts.Length, 1);
    8.     jobHandle.Complete();
    9.     isBad.Dispose();
    10.     ...
    ... but how come the Editor still yells about the array not being disposed of? Is it because Unity is expecting me to use the array for longer than a frame when I'm not using the Temp allocator, and thus dispose of it later? From the Editor, I call into this using an Inspector button and ensure the function is called only once.

    I know I am using the parallel-for incorrectly within a single frame, but I get considerable speed-up using the Burst compilation... so worst-case, I will just use a [BurstCompile] function and iterate as necessary using an Allocator.Temp, which should hopefully bypass this issue.

    OH, and one more thing: the actual function call occurs synchronously on a clone of the object from the original object, before it then immediately marks itself to be destroyed (from Editor, a DestroyImmediate() call). I don't know if that might affect anything, but it is worth mentioning.
     
  2. kanomeister

    kanomeister

    Joined:
    May 16, 2017
    Posts:
    2
    Scratch most of that, I'm just a silly programmer who forgot to do a check somewhere else along this really complicated pipeline. :p