Unity-Technologies / arfoundation-samples

Example content for Unity projects based on AR Foundation
Other
3.02k stars 1.12k forks source link

[Bug] ARPointCloudManager.Append<T>() method throws ArgumentException in the Editor #960

Closed KyryloKuzyk closed 2 years ago

KyryloKuzyk commented 2 years ago

Describe the bug ARPointCloudManager.Append() method throws ArgumentException in the Editor.

To Reproduce Call the ReproduceBug() method in the Editor.

using System.Reflection;
using Unity.Collections;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.XR.ARFoundation;

public static class ARPointCloudManagerBug {
    public static void ReproduceBug() {
        var array1 = new NativeArray<int>(0, Allocator.Temp);
        var array2 = new NativeArray<int>(new int[42], Allocator.Temp);

        var method = typeof(ARPointCloudManager).GetMethod("Append", BindingFlags.NonPublic | BindingFlags.Static);
        Assert.IsNotNull(method);
        var genericMethod = method.MakeGenericMethod(typeof(int));

        try {
            genericMethod.Invoke(null, new object[] {array1, array2, array2.Length, Allocator.Temp});
        } catch (ArgumentException e) {
            Debug.LogError(e);
        } finally {
            array1.Dispose();
            array2.Dispose();
        }
    }
}

Reproducible with Unity Editor [2019.2, 2021.2] AR Foundation [3.0.1, 5.0.0-pre.8]

Proposed solution Add the currentArray.Length parameter to this line: NativeArray<T>.Copy(currentArray, dstArray, currentArray.Length);

andyb-unity commented 2 years ago

Fixed in AR Foundation 5.0.0-pre.12 https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@5.0/changelog/CHANGELOG.html