Search Unity

How can i run long jobs with NativeArray/NativeList parameter?

Discussion in 'Entity Component System' started by Alex1337rus, Feb 16, 2020.

  1. Alex1337rus

    Alex1337rus

    Joined:
    Mar 30, 2016
    Posts:
    37
    With this code i have 3 warnings.
    • Internal: JobTempAlloc has allocations that are more than 4 frames old - this is not allowed and likely a leak
    • To Debug, enable the define: TLA_DEBUG_STACK_LEAK in ThreadsafeLinearAllocator.cpp. This will output the callstacks of the leaked allocations.
    • Internal: deleting an allocation that is older than its permitted lifetime of 4 frames (age = 6)
    Unity 2018.4.17f1 (64-bit).
    But I am using Allocator.Persistent - not Temp or TempJob.
    Code (CSharp):
    1. using UnityEngine;
    2. using Unity.Collections;
    3. using Unity.Jobs;
    4. using Unity.Burst;
    5.  
    6. using static Unity.Mathematics.math;
    7.  
    8.  
    9. [BurstCompile]
    10. struct FindPathJob : IJob
    11. {
    12.     public long summ;
    13.     public NativeList<long> result;
    14.  
    15.     public void Execute()
    16.     {
    17.         const long LIMIT = 10000000;
    18.  
    19.         result.Clear();
    20.         for (long i = 1; i < LIMIT; ++i)
    21.         {
    22.             summ += 6 * i * i + 5 * i * i * i  - 2 * i * (i - 123);
    23.  
    24.             summ += (int)sqrt(summ * i * 1);
    25.  
    26.             summ *= 123 ^ i * i  - 5;
    27.         }
    28.  
    29.         result.Add(summ);
    30.     }
    31. }
    32.  
    33. public class Pathfinding : MonoBehaviour {
    34.  
    35.     const int MAX_JOBS = 1;
    36.  
    37.     JobHandle jobHandle;
    38.     NativeList<long> result;
    39.  
    40.     private void Update()
    41.     {
    42.         if (!jobHandle.IsCompleted)
    43.         {
    44.             return;
    45.         }
    46.  
    47.         jobHandle.Complete();
    48.         if (result.IsCreated)
    49.         {
    50.             Debug.Log("completed " + " " + result[0]);
    51.             result.Dispose();
    52.         }
    53.        
    54.         result = new NativeList<long>(Allocator.Persistent);
    55.  
    56.         FindPathJob findPathJob = new FindPathJob
    57.         {
    58.             result = result
    59.         };
    60.  
    61.         jobHandle = findPathJob.Schedule();
    62.     }
    63.  
    64.     private void OnDestroy()
    65.     {
    66.         jobHandle.Complete();
    67.  
    68.         if (result.IsCreated)
    69.         {
    70.             result.Dispose();
    71.         }
    72.     }
    73. }
    74.