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.
  2. Dismiss Notice

Question NativeArray deallocated after a single use

Discussion in 'C# Job System' started by DevDunk, Jun 19, 2023.

  1. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    I switched to using GetVertexData in order to improve performance.
    I use the array I get out of it in 2 jobs, scheduled after each other (with dependencies to not conflict)

    The first job schedules fine with my vertex array, but the second one does not. Here I get the error
    InvalidOperationException: The UNKNOWN_OBJECT_TYPE CalculateBoundsJob.vertices has been deallocated. All containers must be valid when scheduling a job.


    No idea why this happens, as the job right above it does work. Does anyone have any idea?
    Does GetVertexData automatically deallocate after 1 job?
    in both jobs the array is ReadOnly

    Code (CSharp):
    1.  
    2. public struct BendVertexInfo //Struct used for GetVertexData
    3. {
    4.     public float3 position, normal;
    5.     public float2 uv;
    6.  
    7.     public BendVertexInfo GetNewVertex(float3 pos, float3 norm)
    8.     {
    9.         return new BendVertexInfo() { position = pos, normal = norm, uv = uv };
    10.     }
    11. }
    12.  
    13.  
    14. private void ScheduleBendJob(float3 transformPos)
    15.         {
    16.             curveJobHandle.Complete();
    17.             boundsJobHandle.Complete();
    18.  
    19.             curveJob = new CalculateCurvatureJob()
    20.             {
    21.                 OgVertexInfo = ogVertexInfo,
    22.                 vertexInfo = newVertexInfo, //Schedules fine
    23.                 VertexOffset = vertexOffset,
    24.                 VertexCurvePoint = vertexCurvePoint,
    25.                 CurvatureInfo = new float3x4(StationairyAttatchmentPoint, bendPoint, transformPos, startTangent),
    26.             };
    27.  
    28.             curveJobHandle = curveJob.Schedule(newVertices.Length, 64);
    29.  
    30.             boundsJob = new CalculateBoundsJob()
    31.             {
    32.                 vertices = newVertexInfo, //Error
    33.                 pos = boundPos,
    34.                 size = boundSize,
    35.             };
    36.  
    37.             boundsJobHandle = boundsJob.Schedule(curveJobHandle);
    38.  
    39.             JobHandle.ScheduleBatchedJobs();
    40.         }

    EDIT: Calling
    ogMesh.SetVertexBufferData<BendVertexInfo>(newVertexInfo, 0 ,0, ogMesh.vertexCount);
    later in the code also gives a null error sadly
     
    Last edited: Jun 19, 2023
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,899
    How do you get or create newVertexInfo?
    Do you call Dispose on it anywhere?
    Does it have an attribute that makes it automatically dispose with the job?
    Do you "apply" the (writable) mesh data? Because that will also dispose the collections obtained from MeshData.
     
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,362
    Thanks!
    I found out that the readable mesh data needs to stay in memory in order for the vertex data to stay in memory as well. Just weird that it only errored out on the 2nd job.

    I am trying to use Get/SetVertexBufferData right now without MeshData on the setting side, since I thought that allocating mesh data every frame would hurt performance. now I think about it it might be faster than using .SetVertices and .SetNormals separately

    SetVertexBufferData does not seem to work for me without meshdata atm, so gotta rewrite my code a bit/

    Edit: docs say SetVertexBufferData should work without meshdata, back to the drawing board