BlobArray to NativeArray

Not sure what I’m doing wrong here. source has a BlobArray and BlobArray. The pointer should be good here being I can index into it via the blob array just fine. The debug statements all return correct values up until the last one where it throws a null reference on CheckElementReadAccess.

public unsafe void GetData(out NativeArray<int> indices, out NativeArray<float3> vertices)
        {
            ref var source = ref MeshData.Value;
           
            Debug.Assert(source.Vertices.GetUnsafePtr() != null);
            Debug.LogFormat("vert:{0} ind:{1} value:{2}", source.Vertices.Length, source.Indices.Length, source.Vertices[source.Vertices.Length - 1]);
            indices = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<int>(source.Indices.GetUnsafePtr(), source.Indices.Length, Allocator.Temp);
            vertices = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<float3>(source.Vertices.GetUnsafePtr(), source.Vertices.Length, Allocator.Temp);
            Debug.LogFormat("length:{0}", vertices.Length);
            Debug.LogFormat("value:{0}", vertices[0]);
        }

This works.

public unsafe void GetData3(out NativeArray<int> indices, out NativeArray<float3> vertices)
        {
            ref var source = ref MeshData.Value;

            indices = new NativeArray<int>(source.Indices.Length, Allocator.Temp);
            UnsafeUtility.MemCpy(indices.GetUnsafePtr(), source.Indices.GetUnsafePtr(), indices.Length * UnsafeUtility.SizeOf<int>());

            vertices = new NativeArray<float3>(source.Vertices.Length, Allocator.Temp);
            UnsafeUtility.MemCpy(vertices.GetUnsafePtr(), source.Vertices.GetUnsafePtr(), vertices.Length * UnsafeUtility.SizeOf<float3>());
        }
1 Like

Ah ok just found a post saying you have to setup the atomic safety handle also. Untested but assuming that’s the issue.

Yep, just tested it