Search Unity

IL2CPP compile error with NativeList<T> and IJobParallelFor

Discussion in 'Entity Component System' started by LennartJohansen, Jan 23, 2019.

  1. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    Hi.

    I am experiencing an error when compiling jobs on IL2CPP.
    This is on Unity 2018.3.2 with the latest packages but happens on earlier versions also.

    In the example I create a NativeList that is passed to a IJobParallellFor jo as a deferred NativeArray.
    It does a simple add to the int in the array and tries to read the result.

    The problem seems to be when adding the NativeList as parameter to the parallell job Schedulke to tell it the job length/array count.

    Code (csharp):
    1.  
    2. JobHandle handle = addJob.Schedule(sourceList ,1);
    3.  
    This works good in the editor and compiled with mono but gives an NullReferenceException on IL2CPP compiled standalone builds. See error below.

    running the same code with a hardcoded length in the Schedule function works good.

    It does not seem to be related to the burst compiler. I get an error with and without the
    [BurstCompile] tag.

    Any ideas on a workaround or fix?

    Lennart

    Code (csharp):
    1.  
    2. using Unity.Burst;
    3. using Unity.Collections;
    4. using Unity.Jobs;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. //[BurstCompile]
    9. public struct AddJob : IJobParallelFor
    10. {
    11.     public NativeArray<int> SourceArray;
    12.  
    13.     public void Execute(int index)
    14.     {
    15.         SourceArray[index] += 1;
    16.     }
    17. }
    18.  
    19. public class Testscript : MonoBehaviour
    20. {
    21.     public Text ResultText;
    22.  
    23.     void Update()
    24.     {
    25.         NativeList<int> sourceList = new NativeList<int>(Allocator.TempJob);
    26.         sourceList.Add(0);
    27.  
    28.         AddJob addJob = new AddJob
    29.         {
    30.             SourceArray = sourceList.AsDeferredJobArray()
    31.         };
    32.  
    33.         JobHandle handle = addJob.Schedule(sourceList ,1);
    34.         handle.Complete();
    35.  
    36.         ResultText.text ="Result: " + sourceList[0].ToString();
    37.         sourceList.Dispose();
    38.     }    
    39. }
    40.  

    Code (csharp):
    1.  
    2.  
    3. NullReferenceException: Object reference not set to an instance of an object.
    4.   at Unity.Jobs.IJobParallelForDeferExtensions+ParallelForJobStruct`1[T].Initialize () [0x00000] in <00000000000000000000000000000000>:0
    5.   at Unity.Jobs.IJobParallelForDeferExtensions.Schedule[T,U] (T jobData, Unity.Collections.NativeList`1[T] list, System.Int32 innerloopBatchCount, Unity.Jobs.JobHandle dependsOn) [0x00000] in <00000000000000000000000000000000>:0
    6.   at Testscript.Update () [0x00000] in <00000000000000000000000000000000>:0
    7.  
    8.  
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    I made a bug report with the sample project. Case 1119844
     
    Spy-Shifty likes this.
  3. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    Unity was able to reproduce it.
     
    Spy-Shifty likes this.
  4. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394