Search Unity

Using NativeList Crash Editor

Discussion in 'Entity Component System' started by Rewaken, Jan 14, 2019.

  1. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    128
    Hello there, I am not completely sure why but using native list crash editor. I ran out of idea what I am doing wrong.

    Code (CSharp):
    1. [BurstCompile]
    2. struct VelocityJob : IJobParallelFor
    3. {
    4.  
    5.     [ReadOnly]
    6.     public NativeList<int> MyValue;
    7.     public void Execute(int i)
    8.     {
    9. MyValue[0] += i;
    10. }
    11. }
    With following code editor did not crash but freeze
    Code (CSharp):
    1. public NativeList<int> MyValue;
    2.  
    3. private void Start()
    4.     {
    5.         MyValue = new NativeList<int>( Allocator.Temp);
    6.         MyValue.Add(10);
    7.      
    8. }
    And if I allocate with for loop in start editor crash
    Code (CSharp):
    1.  for (int i = 0; i < 100; i++)
    2.         {
    3.               MyValue.Add(i);
    4.         }
    5.        
    Can anyone tell me how to use nativelist
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    You're marking your list as read only in the VelocityJob then writing to it. Not sure why this isn't throwing a warning though instead of crashing.
     
  3. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    Also he increments same index in parallel which gives unexpected results.
     
  4. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    128
    Earlier,When I didnt mark list as read only unity threw following exception telling me to add readonly marker on list.

    InvalidOperationException: VelocityJob.MyValue is not declared [ReadOnly] in a IJobParallelFor job. The container does not support parallel writing. Please use a more suitable container type.
    Even after changing line
    Code (CSharp):
    1. MyValue[0] += i;
    To
    Code (CSharp):
    1. MyValue[i] += i;
     
  5. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    If you write in parallel, and you can guarantee safety, you must use NativeDisableParallelForRestriction attribute, or rethink logic to Concurrent containers.
     
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    What he is currently doing is definitely not parallel safe though!
     
  7. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,685
    I know :) And this is why I made this remark:
     
  8. Rewaken

    Rewaken

    Joined:
    Mar 24, 2015
    Posts:
    128
    Thanks adding attribute removes the error. but I still cant understand that why adding with for loop crash unity
    Code (CSharp):
    1.  for (int i = 0; i < position.Length; i++)
    2.         {
    3.                        MyValue.Add(i);
    4.         // Crash Editor
    5.         }
    But this works
    Code (CSharp):
    1.             MyValue.Add(10);
    2.        
    I am scheduling job with position length
    Code (CSharp):
    1.              
    2. var job = new VelocityJob()
    3.                 {
    4.                  position = position,
    5.                  MyValue = MyValue,
    6.                    };
    7.                  jobHandle = job.Schedule(position.Length, 64);
    8.