IndexOutOfRangeException in NativeArray crashes the game

When I try to:
entityManager.SetComponentData(entites[i], something) (i > entities.Length)

Game crashes with very unobvious stack trace:

0x00007FFC3556809B (GameAssembly) [\Temp\StagingArea\Data\il2cppOutput\GenericMethods16.cpp:36900] EntityDataAccess_SetComponentData_TisLocalToParent_t8CE5D64D0EC85245F7D757E7BB18E8047C8186E6_m73BA6A016ADDE9AF9FCF09CC489728972A6C192C_gshared
0x00007FFC35E898FC (GameAssembly) [\Temp\StagingArea\Data\il2cppOutput\GenericMethods26.cpp:18300] EntityManager_SetComponentData_TisStreamingState_tAB946E8FD261942E6D479659E227173C874EF9C7_mF8006A62BF008B475F681946660B250D91214332_gshared
0x00007FFC36B28F17 (GameAssembly) [\Temp\StagingArea\Data\il2cppOutput\BA.SoundSamples.cpp:20093] SoundSamplesPreviewPlayer_SpawnSamplesTestingForBeat_m03DB91776A479C086DC4A5A9BEDEAAB75C736921
0x00007FFC35608DFE (GameAssembly) [\Temp\StagingArea\Data\il2cppOutput\BA.Plugins4.cpp:46307] OnCancel_Invoke_mA50AD69FD0972515C450A16F43862FF74953364F

It happens in the build (Windows il2cpp x64). In editor I get normal IndexOutOfRangeException.

Here’s my use case for anyone interested:

ECS is used to spawn 30k+ quads to represent sound samples.
Jobs System used to process audio using custom FFT.

That’s because in a built game safety checks are disabled, so in the editor the safety check is throwing that error and in the game you are just flat out violating the bounds and causing a hard crash.

Really cool concept by the way!

Thanks!

Is it possible to make a build with safety checks enabled? At least during testing (and on Steam beta branch).

From il2cpp:

If that’s the same case in ecs and it’s disabled by default then it’s getting problematic. My game was crashing in weird places like AudioSettings.Reset().

Not sure if it matters but that entityManager.SetComponentData is called from main thread in MonoBehaviour - burst/systems not used in that case.

Since you are able to reproduce the issue in the editor you are better off fixing your bug.

Well I’m not sure, there were few issues. I fixed all that could be found in the editor. I guess I’ll make my own length check with defines for testing builds:

#if NATIVE_ARR_LEN_CHECK
if( i >= nativeArray.Length )
    Debug.LogError( $"{i} of {nameof(nativeArray)} is out of range");
#endif
nativeArray[i] = ...

Extension method would be cleaner but probably very slow with how il2cpp handles them.

You can turn on safety in build

But yes you should fix this, you really don’t want safety on in build.

2 Likes