Skip to main content
2 of 5
Cleanup
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

AsyncGPUReadback.RequestIntoNativeArray - owner has been invalidated

I have the following C# code in Unity:

private void Start()
{
    CreateBuffer(ref _data, ref _rt);
    StartCoroutine(StartEncoding(_rt,isLoging));
}

System.Collections.IEnumerator StartEncoding(RenderTexture oRTs, bool active)
{       
    while (active)
    {          
        yield return new WaitForEndOfFrame();
        
        AsyncGPUReadback.RequestIntoNativeArray(ref _data, oRTs, 0, request =>
        {
            if (request.hasError) {
                Debug.Log("GPU readback error detected."); 
                return;
            }
            Debug.Log("in Function ");
            // encode to png and save 
            Debug.Log( "saved PNG file ");           
        });
    }
}

Before even the first frame ends I get this error:

1[System.Byte] can no longer be accessed, since its owner has been invalidated.

What is happening? I can get date to be used once and then I cannot use it anymore?

My question is how can I use AsyncGPUReadback.RequestIntoNativeArray correctly so I do not lose access to my NativeArray?

I would like to hold on my data and pass it to Job so inside job i can do encoding to PNG .