Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NativeArray doesn't implement INativeDisposable?

Discussion in 'Entity Component System' started by Jpritch71, May 20, 2022.

  1. Jpritch71

    Jpritch71

    Joined:
    Mar 4, 2014
    Posts:
    11
    I've made a little convenience class to help myself dispose of Native Collections after jobs:


    Code (CSharp):
    1.     public static class Disposal
    2.     {
    3.         public static void Schedule
    4.         (
    5.             in JobHandle handle,
    6.             params INativeDisposable[] disposables
    7.         )
    8.         {
    9.             foreach (var disposable in disposables) disposable.Dispose(handle);
    10.         }
    11.     }
    I can use it like so:


    Code (CSharp):
    1.             var map = new NativeMultiHashMap<Entity, SomeStruct>(count, Allocator.TempJob);
    2.             var list = new NativeList<Entity>(Allocator.TempJob);
    3.             var set = new NativeHashSet<Entity>(count, Allocator.TempJob);
    4.             Dependency = new JobThatDoesSomething
    5.             {
    6.                 List=list,
    7.                 Map=map,
    8.                 Set=set
    9.             }.Schedule(list.Length, 1, Dependency);
    10.             Disposal.Schedule(Dependency, map, list, set);
    It's not critical to anything, I just made it to save myself a tiny bit of trouble when disposing a bunch of different collections after a job. However, I noticed today that this doesn't work with NativeArray, as for some reason it does not implement INativeDisposable, even though it DOES support disposal after a job. That is to say, NativeArray does match the interface for INativeDisposable, but it does not implement it and therefore can't be used in my helper function.

    Is there a reason for this, or is it an oversight that should be reported as a bug?
     
  2. elcionap

    elcionap

    Joined:
    Jan 11, 2016
    Posts:
    138
    Just because INativeDisposable was created for the Unity.Collections package and NativeArray is bound to Unity core itself. So unless/until they move INativeDisposable to the Unity core, NativeArray can't implement INativeDisposable.

    []'s