Search Unity

Resolved Job: NullReferenceException on Burst Compilation

Discussion in 'Entity Component System' started by EternalAmbiguity, May 8, 2022.

  1. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    I have a Job in Unity. Whenever I run it with Burst compilation enabled, it causes a NullReferenceException. Whenever I run it without Burst compilation, it's fine.

    burst_null_2.png

    I commented out most of it in an attempt to identify what specifically causes it, but it happens every time the method returning the JobHandle is called (so
    updateProbsHandle = ScheduleProbsUpdate(/*a, updateLocation, jobProbs*/);
    ). See all below:

    Code (csharp):
    1. public partial class CASystem : SystemBase
    2. {
    3.     static NativeQueue<UnsafeList<int>> updateLocations = new NativeQueue<UnsafeList<int>>(Allocator.Persistent);
    4. ...
    5.     protected override void OnUpdate()
    6.     {
    7.         JobHandle updateProbsHandle = new JobHandle();
    8.         NativeHashMap<int, double> jobProbs = new NativeHashMap<int, double>(0, Allocator.Persistent);
    9.         bool updatedProbs = false;
    10.         if (updateLocations.IsCreated && updateLocations.TryDequeue(out var updateLocation))
    11.         {
    12.             if (!updateLocation.IsCreated)
    13.             {
    14.                 Debug.LogError("update not created?");
    15.             }
    16.                 updatedProbs = true;
    17.             //for (int i = 0; i < updateLocation.Length; i++)
    18.             //{
    19.             //    jobProbs.TryAdd(updateLocation[i], 0);
    20.             //}
    21.             //var a = probsFull.GetValueArray(Allocator.TempJob).AsReadOnly();
    22.             updateProbsHandle = ScheduleProbsUpdate(/*a, updateLocation, jobProbs*/);
    23.         }
    24.     }
    25. ...
    26.     public static JobHandle ScheduleProbsUpdate(/*NativeArray<UnsafeHashMap<int, double>>.ReadOnly inputs, UnsafeList<int> positions, NativeHashMap<int, double> outputs*/)
    27.     {
    28.         JobHandle jobhandle = new UpdateProbsJob
    29.         {
    30.             //inputs = inputs,
    31.             //positions = positions,
    32.             //output = outputs
    33.         }.Schedule();
    34.  
    35.         return jobhandle;
    36.     }
    37.     [BurstCompile(CompileSynchronously = true)]
    38.     public struct UpdateProbsJob : IJob
    39.     {
    40.         //[ReadOnly] public NativeArray<UnsafeHashMap<int, double>>.ReadOnly inputs;
    41.         //[ReadOnly] public UnsafeList<int> positions;
    42.  
    43.         //public NativeHashMap<int, double> output;
    44.  
    45.         public void Execute()
    46.         {
    47.             //if (!positions.IsCreated)
    48.             //{
    49.             //    Debug.LogError("positions not created?");
    50.             //}
    51.             //if(!output.IsCreated)
    52.             //{
    53.             //    Debug.LogError("output not created?");
    54.             //}
    55.             //for (int i = 0; i < positions.Length; i++)
    56.             //{
    57.             //    int index = positions[i];
    58.             //    double p = 0;
    59.             //    for (int j = 0; j < inputs.Length; j++)
    60.             //    {
    61.             //        if(!inputs[j].IsCreated)
    62.             //        {
    63.             //            Debug.LogError("not created?");
    64.             //        }
    65.             //        else
    66.             //        {
    67.             //            if (inputs[j].TryGetValue(index, out double item))
    68.             //            {
    69.             //                if (item == -1)
    70.             //                {
    71.             //                    p = -1;
    72.             //                    break;
    73.             //                }
    74.             //                else if (item > p && p >= 0)
    75.             //                {
    76.             //                    p = item;
    77.             //                }
    78.             //            }
    79.             //        }
    80.             //    }
    81.             //    output[index] = p;
    82.             //}
    83.         }
    84.     }
    As can be seen everything inside the job that uses any inputs at all has been commented out (and nothing is actually passed to the job), but it still gives a NullReferenceException whenever Burst compilation is enabled (and of course it gives the same exception with everything un-commented out).

    Any suggestions?
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Try delete the BurstCache in your library folder. There's a rare bug currently where the cache can be corrupted and a job won't be recompiled.
    The burst team is aware and there should be a fix next release.
     
    Last edited: May 8, 2022
    GreedyVox and EternalAmbiguity like this.
  3. EternalAmbiguity

    EternalAmbiguity

    Joined:
    Dec 27, 2014
    Posts:
    3,144
    Looks like that was it - it's starting up fine now. Thanks!