Search Unity

Question Burst error BC1091: External and internal calls are not allowed inside static constructors

Discussion in 'Entity Component System' started by jwvanderbeck, Oct 11, 2022.

  1. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    IMHO this really needs some tooling improvement. How am I supposed to take that error and figure out what the heck it even means let alone how to fix whatever is causing it?

    The stack just seems to be a dense list of pretty much ALL my job structs - even ones that don't have the BurstCompile attribute, but even if it showed just one job in the stack I still wouldn't have any clue what code IN the job I should be looking at.
     
  2. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    So through a process of just commenting out code until the error went away, I managed to narrow it down to I think the code that creates a new instance of a struct is what causes the error?

    Code (CSharp):
    1. var edge = new Edge(centroidIndex, neighborCentroidIndex);
    Code (CSharp):
    1.     public struct Edge : IEquatable<Edge>
    2.     {
    3.         public int A, B;
    4.  
    5.         public Edge(int a, int b)
    6.         {
    7.             A = math.min(a, b);
    8.             B = math.max(a, b);
    9.         }
    10.  
    11.         public bool Equals(Edge other)
    12.         {
    13.             return A == other.A && B == other.B;
    14.         }
    15.  
    16.         public override bool Equals(object obj)
    17.         {
    18.             return obj is Edge other && Equals(other);
    19.         }
    20.  
    21.         public override int GetHashCode()
    22.         {
    23.             return HashCode.Combine(A, B);
    24.         }
    25.     }
    26.  
     
  3. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    Context what method this code is in and maybe even the path leading to that method call would help and/or the full callstack of the error.

    I don't think Edge class is the issue, it's where you're instantiating it. As I see it "static constructor" is the thing to watch out for. Check any classes that have a static constructor. Perhaps this could also relate to methods attributed with [InitializeOnLoadMethod].
     
  4. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    Like I said the stack was really non helpful but here it is for reference.

    (0,0): Burst error BC1091: External and internal calls are not allowed inside static constructors: Interop.BCrypt.BCryptGenRandom(System.IntPtr hAlgorithm, byte* pbBuffer, int cbBuffer, int dwFlags)

    While compiling job:
    Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[[TiledSphere.Jobs.CalculateMidPointsJob, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null::Execute(TiledSphere.Jobs.CalculateMidPointsJob&, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|Unity.Jobs.LowLevel.Unsafe.JobRanges&, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
    Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[[TiledSphere.Jobs.CalculateCenterPointsJob, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null::Execute(TiledSphere.Jobs.CalculateCenterPointsJob&, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|Unity.Jobs.LowLevel.Unsafe.JobRanges&, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
    Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[[TiledSphere.Jobs.CalculateNeighborsJob, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null::Execute(TiledSphere.Jobs.CalculateNeighborsJob&, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|Unity.Jobs.LowLevel.Unsafe.JobRanges&, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
    Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[[TiledSphere.Jobs.GenerateHexMeshJob, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]], UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null::Execute(TiledSphere.Jobs.GenerateHexMeshJob&, TiledSphere, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|System.IntPtr, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089|Unity.Jobs.LowLevel.Unsafe.JobRanges&, UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null|System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
     
  5. jwvanderbeck

    jwvanderbeck

    Joined:
    Dec 4, 2014
    Posts:
    825
    Thanks. I know NOTHING about hashcodes but found something on SO that I think will work and compiles.

    That's great that they are fixing this specific issue but my post was more about the fact that the error is completely opaque I think to most users. Half of your reply didn't even make any sense to me :D

    Unity has always been about opening up the power to create to more people, not less, and this is just IMHO something that is overly technical and hard to understand - the very type of thing DOTS is supposed to be helping with :) So some extra tooling here to better help the developer understand where the error is, what the error actually means, and how to fix it would be great :)

    Just my feedback there. Replacing the HashCode.Combine though with some magic code I got off SO seems to have worked though. Here is that code for anyone else (or future me) that runs into this and Googles:

    Code (CSharp):
    1.          
    2. public override int GetHashCode()
    3. {
    4.     unchecked // Overflow is fine, just wrap
    5.     {
    6.         int hash = 17;
    7.         // Suitable nullity checks etc, of course :)
    8.         hash = hash * 23 + A.GetHashCode();
    9.         hash = hash * 23 + B.GetHashCode();
    10.         return hash;
    11.     }
    12. }    
    13.  
     
  6. Trindenberg

    Trindenberg

    Joined:
    Dec 3, 2017
    Posts:
    398
    I also get confused by the errors, but I think it refers to the item at the top of the 'stack' TiledSphere.Jobs.CalculateMidPointsJob? Something marked static that is calling another method. Maybe...