Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

AsyncGPUReadback.RequestIntoNativeArray - What am I doing wrong?

Discussion in 'Scripting' started by jhelbig, Oct 15, 2020.

  1. jhelbig

    jhelbig

    Joined:
    Jul 25, 2019
    Posts:
    3
    Hi,

    I'm currently using AsyncGPUReadback.RequestIntoNativeArray(ref array, ...) to asynchronously read the data of a RenderTexture I'm blitting to every few frames. I'm using a NativeArray in an effort to reduce recurring garbage allocations. So far so good. I got everything to work like I want it to, but it seems like I'm still doing something wrong with the NativeArray I'm passing to the method.

    I'm allocating a NativeArray with Allocator.Persistent, pass that to RequestIntoNativeArray() and use that NativeArray to do some other stuff after the AsyncGPUReadbackRequest has completed. When quitting the game I try to clean up after myself and call Dispose() on the persistent array.

    Sometime the following problem occurs:

    When I stop my application in the Editor the following exception is thrown:
    "InvalidOpertationException: [NativeArray...] has been set to undisposable and cannot be deallocated"

    And when I hit Play again after that:
    "A Native Collection has not been disposed, resulting in a memory leak. Allocated from: ..."

    Both exceptions point to the array I mentioned above. This seems somewhat contradictory to me.
    Does anyone have a clue as to what the issue is, if there is any?
     
    PraetorBlue and landonth like this.
  2. landonth

    landonth

    Joined:
    Dec 3, 2018
    Posts:
    141
    PraetorBlue likes this.