NativeArrays get deallocated on job scheduled

Hello everyone,

I’m attempting to start an IJobParallelFor derived stuct, which uses 3 NativeArrays of Vector 3.

Two of them are allocated with copies of the same set of mesh vertices, the third one is an array of the same size as that vertices collection (so initialised with size and allocation type).

All three of them are initialised with Allocator.Persistent, however, as soon as I schedule the job all three of them become inaccessible with this error:

This is on the very first iteration and it’s totally equivalent to the code sample contained in this Unity doc page

I’m not sure what I’m doing wrong, I’d appreciate any suggestions for how to attempt to debug this error.

Sounds like you are trying to access it after myArray.Dispose possibly. Can’t say much without seeing the code. Maybe you’re creating the persistent array outside of Update (also not sure why Persistent is needed in the example since it’s disposed at the end).

Hey, as I stated it is being accessed before they got disposed.

My original plan was to not dispose them at all, as they’re meant to be persistent sets of data to store a mesh state I can then incrementally change as frames go by.

What I noticed with the debugger is that those NativeArrays, regardless of what Allocator I use to reserve the memory, will appear as “disposed” immediately after the line where I schedule the job.

So arrays are there the line before, correctly initialised.
They’re still there when I hit the Schedule line.
They appear as “disposed” after the job is scheduled

This is all happening in editor, I’ll reiterate that I wrote the code so that it’s perfectly equivalent to the code sample for IJobParallelFor you can find in the unity docs, so it should work.

Some stuff that could help us help you:
-Specify the Unity version you’re using. Sometimes, people stick to old versions for reasons of varying quality. The older versions can then have
-Share the exact code that you used. “Perfectly equivalent” isn’t very helpful, if you can reproduce the issue with the code sample you mentioned, then say that, otherwise we don’t have sufficient information on what the source of the problem could be.

As above but additionally, the only other things that come to mind is that (somehow) the error or what you’re checking are not the same arrays or that there’s DeallocateOnJobComplete in there and the job is finishing quickly before you subsequently test it.

But yes, a description of an issue doesn’t reveal much beyond wild speculation.

This is the code I’m using for the job.
Unity version: 2021.3.11f1

var m_OriginalVerticesNative = new NativeArray<Vector3>(m_OriginalVertices, Allocator.Persistent);
                var m_DisplacedVerticesNative = new NativeArray<Vector3>(m_OriginalVertices, Allocator.Persistent);
                var m_VertexVelocitiesNative = new NativeArray<Vector3>(m_OriginalVertices.Length, Allocator.Persistent);

                VertexDisplacementJob job = new VertexDisplacementJob()
                {
                    VertexVelocities = m_VertexVelocitiesNative,
                    DisplacedVertices = m_DisplacedVerticesNative,
                    OriginalVertices = m_OriginalVerticesNative,
                    SpringForce = m_SpringForce,
                    Damping = m_Damping,
                    Deltatime = Time.deltaTime
                };
                // All arrays are allocated and inspectable with the debugger
                m_Handle = job.Schedule(m_OriginalVertices.Length, m_BatchSize);

                // Arrays are no longer inspectable with the debugger, as if they had been disposed
                m_Handle.Complete();
                // This launches an exception, as DisplacedVertices (as well as the others) appear to have been disposed
                m_DeformingMesh.vertices = job.DisplacedVertices.ToArray();
              
                m_OriginalVerticesNative.Dispose();
                m_DisplacedVerticesNative.Dispose();
                m_VertexVelocitiesNative.Dispose();

I doubt the Unity version is at fault here, as we use a package which already uses jobs

What if you use NativeArray.Copy(m_DisplacedVerticesNative, m_DeformingMesh.vertices) instead?

Does the same happen if you use “Run” rather than “Schedule”? Try passing it to another test job (do nothing with the array), schedule it and complete and see if the same happens.

If it’s something in the job, is it doing something unsafe that you know of i.e. some kind of subtle corruption?

Thanks for the replies, however I won’t be able to quickly test the changes as I’ve been moved to a different project and that specific task has been put on hold.

I’ll do some more testing in the next days/weeks whenever I get some time to go deeper.
Apologies.