Search Unity

Bug ResizeArray from NativeArray<T> is not correctly implemented

Discussion in 'Entity Component System' started by PauSolsona, Nov 24, 2022.

  1. PauSolsona

    PauSolsona

    Joined:
    Jun 16, 2022
    Posts:
    1
    While working around with ECS I've found this bug. Found in Library\PackageCache\com.unity.render-pipelines.core@12.1.7\Runtime\Utilities\ArrayExtensions.cs

    The current code works like this:

    Code (CSharp):
    1.        
    2.         /// <summary>
    3.         /// Resizes a native array. If an empty native array is passed, it will create a new one.
    4.         /// </summary>
    5.         /// <typeparam name="T">The type of the array</typeparam>
    6.         /// <param name="array">Target array to resize</param>
    7.         /// <param name="capacity">New size of native array to resize</param>
    8.         public static void ResizeArray<T>(this ref NativeArray<T> array, int capacity) where T : struct
    9.         {
    10.             var newArray = new NativeArray<T>(capacity, Allocator.Persistent, NativeArrayOptions.UninitializedMemory);
    11.             if (array.IsCreated)
    12.             {
    13.                 NativeArray<T>.Copy(array, newArray, array.Length);
    14.                 array.Dispose();
    15.             }
    16.             array = newArray;
    17.         }
    18.  
    However, since the function of this code is to resize the array, on line 13, instead of copying with the lenght of the old array, it should copy with the length of the smallest length. That way it would work to increase the size and to reduce the size.
     
  2. vectorized-runner

    vectorized-runner

    Joined:
    Jan 22, 2018
    Posts:
    398
    Decreasing the Length by resizing doesn't make sense for Array anyway. If it was a List you could decrease the Capacity but keep the Length same.