InvalidOperationException: The UNKNOWN_OBJECT_TYPE has been deallocated, it is not allowed to access

Hi I am getting this error when running my program, very randomly and I am not sure what is causing the problem.

I have a suspicion that it is because of this line:

        NativeArray<Color32> texturePixels = texture.GetPixelData<Color32>(0);  // this line
        var deltaTime = Time.DeltaTime;
   
        Entities.ForEach((ref PixelEntity pixelEntity, ref RequiresUpdate update, ref Timer updateTimer) =>
        {

I am wondering if the NativeArray that is generated from .GetPixelData<Color32>(0) is persistent or not? Does it somehow get deallocated?

So reading the documentation it doesn’t say if it is persistent or not but it does say:

GetPixelData does not allocate memory; the returned NativeArray directly points to the Texture system memory data buffer.

So I am guessing it is persistent. I am thinking that I must be holding onto the array for too long before using the Apply() call. and therefore hitting this error.

It is recommended to immediately use or modify the data retrieved by this method and to not store the returned array for later use as the returned array can become invalid (i.e. it no longer points to valid memory) if the Texture is modified or updated after you called this method.

===============================================

Hmm its calling the error on line 19:

UpdateCellsSystem.OnUpdate () (at Assets/Scripts/Systems/UpdateCellsSystem.cs:19)

which is this line:

Entities.ForEach((ref PixelEntity pixelEntity, ref RequiresUpdate update, ref Timer updateTimer) =>

Can I use array from texture.GetPixelData(0) inside a job?
Method below will throw the same exception if called every frame:

public static void GenerateGrid(..., Texture2D tex)
        {
            tex.Apply(); //even if I comment this line than exception might still occur randomly

            var j = new GenerateGridJob();
//other fields
            j.colors = tex.GetPixelData<Color32>(0);
            var handle = j.Schedule(j.colors.Length, 128); //this line throws exception InvalidOperationException: The UNKNOWN_OBJECT_TYPE GenerateGridJob.colors has been deallocated. All containers must be valid when scheduling a job.
            handle.Complete();
            tex.Apply();
        }