Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-blittable data error occurs with the struct contains Boolean in Windows IL2CPP. (Unity2021 and above) #2

Open
qwe321qwe321qwe321 opened this issue Mar 12, 2024 · 0 comments

Comments

@qwe321qwe321qwe321
Copy link

qwe321qwe321qwe321 commented Mar 12, 2024

I already have the solution. Just open an issue to let other users know.

Error log

ArgumentException: Object contains non-primitive or non-blittable data.
  at Stella3D.SharedArray`2[T,TNative].Initialize () [0x00000] in <00000000000000000000000000000000>:0 
  at NewBehaviourScript.CreateSharedArray[T] () [0x00000] in <00000000000000000000000000000000>:0 
  at NewBehaviourScript.OnGUI () [0x00000] in <00000000000000000000000000000000>:0 

How to reproduce

I made a simple script to reproduce.

void OnGUI()
{
    if (GUI.Button(new Rect(10, 10, 300, 30), "Create NativeArray A"))
    {
        CreateNativeArray<CustomStructA>();
    }
    if (GUI.Button(new Rect(10, 50, 300, 30), "Create SharedArray A"))
    {
        CreateSharedArray<CustomStructA>();
    }
}

private void CreateNativeArray<T>() where T : unmanaged
{
    Debug.Log("CreateNativeArray " + typeof(T));
    // Create a native array.
    NativeArray<T> nativeArray = new NativeArray<T>(10, Allocator.Persistent);
    nativeArray.Dispose();
}

private void CreateSharedArray<T>() where T : unmanaged
{
    Debug.Log("CreateSharedArray " + typeof(T));
    // Create a shared array.
    Stella3D.SharedArray<T> sharedArray = new Stella3D.SharedArray<T>(10);
    sharedArray.Dispose();
}

public struct CustomStructA {
    public bool booleanVar;
}
  1. Attach the script on a gameobject and build a development build in IL2CPP Windows.
  2. Press the button Create SharedArray A and you will see that creating NativeArray<CustomStructA> worked, but SharedArray<CustomStructA> throwed the exception.

Environment:

  • Unity 2021 LTS and above.
  • Windows IL2CPP (It worked properly with Mono)

How does it happen

There is an Unity issue about the same error but occurs calling NativeArray.Copy.
https://issuetracker.unity3d.com/issues/argumentexception-error-occurs-in-copy-method-of-nativearray-class-when-building-with-windows-il2cpp
I think it is the same internal issue but occured in different functions. Since it fixed in 2022.2.19f1, this issue goes away from Unity 2022 as well.

And there is a dicussion about this issue in 2020
https://forum.unity.com/threads/argument-exception-on-windows-il2cpp-build-with-burst-jobs.836059/
As tertle said, IL2CPP and Burst together may have issues with bools. This is how I found out where the issue was coming from.

Struct with bool fields causes this issue.

Solution / Workaround

Add this attribute [MarshalAs(UnmanagedType.U1)] to your boolean fields.

public struct CustomStructA {
    [MarshalAs(UnmanagedType.U1)]
    public bool booleanVar;
}

I found there is bool2 in Unity.Mathematics package and it worked well.
That is the attribute they used for bool struct series.
https://github.com/Unity-Technologies/Unity.Mathematics/blob/master/src/Unity.Mathematics/bool2.gen.cs

@qwe321qwe321qwe321 qwe321qwe321qwe321 changed the title Non-blittable data error occurs with the user struct contains Boolean in Windows IL2CPP. (Unity2021 and above) Non-blittable data error occurs with the struct contains Boolean in Windows IL2CPP. (Unity2021 and above) Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant