NullReferenceException inside of EntityManager.CopyEntitiesFrom

Hi,
i just had a player reporting a nullpointer exception in a build at a very strange place inside of Unity. It was while i prepare for a savegame, so i copy everything of my main world to a separate world with that method, to save asynchronous later on. It just happened once now, but i have no idea what the reason can be.

As an result of this exception, many entities were duplicated in the original world.

I’m using Unity 6 Preview 6000.0.2f1

Exception: NullReferenceException: Object reference not set to an instance of an object
            Unity.Entities.EntityManager.HighestEntityIndex () (at <c31d8af80290455696b1cc43326538c4>:0)
            Unity.Entities.EntityManager.CreateEntityRemapArray (Unity.Collections.AllocatorManager+AllocatorHandle allocator) (at <c31d8af80290455696b1cc43326538c4>:0)
            Unity.Entities.EntityManager.CopyEntitiesFrom (Unity.Entities.EntityManager srcEntityManager, Unity.Collections.NativeArray`1[T] srcEntities, Unity.Collections.NativeArray`1[T] outputEntities) (at <c31d8af80290455696b1cc43326538c4>:0)

Hi there, not sure if it’s the same case but hope it helps.

I did not encounter this in the CopyEntitiesFrom() calls in my test scene but I encountered this in the MoveEntitiesFrom() calls when I was looking for a way to exactly know how many entities are needed to be moved in a NativeArray instead of using an ever growing NativeList.

That said, maybe try to play around the sizes of your source and output array. I also added this before I made the remap call: sourceEntityManager.CompleteAllTrackedJobs();

Again, we might have had completely different issues. Good luck!

1 Like

Hi, thanks for the hint, problem is that i can’t reproduce it, it just happened one time after thousands of hours total playtime, with a lot of calls to this method… But i added you suggestion, hope its not happening again :slight_smile:

@someoneFromUnity, maybe someone of you can check possible problems in the unity code that can cause that?

1 Like

Adding this here, maybe it can help. This is in EntityManager.cs ln 217

// TODO : this is a temporary workaround for the use of EntityCapacity in remapping
// NOTE : this only accounts for entities which are actually stored in chunks,
// not the ones which are merely referenced by chunks (aka external references)
public int HighestEntityIndex()
{
    int maxIndex = 0;
    var access = GetCheckedEntityDataAccess();
    var archetypes = access->EntityComponentStore->m_Archetypes;

    for (int archetypeIndex = 0, archetypeCount = archetypes.Length; archetypeIndex < archetypeCount; archetypeIndex++)
    {
        var chunks = archetypes[archetypeIndex]->Chunks;
        for (int chunkIndex = 0, chunkCount = chunks.Count; chunkIndex < chunkCount; chunkIndex++)
        {
            var chunk = chunks[chunkIndex];
            var entities = (Entity*)chunk.Buffer;
            for (int entityIndex = 0, entityCount = chunk.Count; entityIndex < entityCount; entityIndex++)
            {
                var index = entities[entityIndex].Index;
                if (index > maxIndex)
                {
                    maxIndex = index;
                }
            }
        }
    }

    return maxIndex;
}
2 Likes

Maybe related to that: Players reporting crashes. I found in the stacktrace also this method…

========== OUTPUTTING STACK TRACE ==================

0x00007FFD0B7A1AF2 (lib_burst_generated) burst.initialize.statics.09a7642d475d3f87ea772b19dfed0fbe_avx2
0x00007FFD0B7A17FC (lib_burst_generated) burst.initialize.statics.09a7642d475d3f87ea772b19dfed0fbe_avx2
0x00007FFD0BAAE1B3 (lib_burst_generated) burst.initialize.statics.5db5b09b58b48d4937bfdb31e46bcebc_avx2
0x00007FFD0BB40ED7 (lib_burst_generated) 77c83703e84b094c789a212b7183772e
0x000001AB43383DF9 (Mono JIT Code) (wrapper managed-to-native) Unity.Entities.StructuralChange/Unity.Entities.InstantiateEntities_00000F9F$BurstDirectCall:wrapper_native_indirect_000001AB3776B120 (intptr&,Unity.Entities.EntityComponentStore*,Unity.Entities.Entity*,Unity.Entities.Entity*,int,bool)
0x000001AB43383AD3 (Mono JIT Code) Unity.Entities.StructuralChange/Unity.Entities.InstantiateEntities_00000F9F$BurstDirectCall:Invoke (Unity.Entities.EntityComponentStore*,Unity.Entities.Entity*,Unity.Entities.Entity*,int,bool)
0x000001AB433839F3 (Mono JIT Code) Unity.Entities.StructuralChange:InstantiateEntities (Unity.Entities.EntityComponentStore*,Unity.Entities.Entity*,Unity.Entities.Entity*,int,bool)
0x000001AB43383963 (Mono JIT Code) Unity.Entities.EntityDataAccess:InstantiateInternalDuringStructuralChange (Unity.Entities.Entity*,Unity.Entities.Entity*,int,int,bool,Unity.Entities.SystemHandle&)
0x000001AB433836BB (Mono JIT Code) Unity.Entities.EntityManager:CopyEntitiesInternal (Unity.Collections.NativeArray`1<Unity.Entities.Entity>,Unity.Collections.NativeArray`1<Unity.Entities.Entity>)
0x000001AB43382E63 (Mono JIT Code) Unity.Entities.EntityManager:CopyEntitiesFrom (Unity.Entities.EntityManager,Unity.Collections.NativeArray`1<Unity.Entities.Entity>,Unity.Collections.NativeArray`1<Unity.Entities.Entity>)
0x000001AB43380C7B (Mono JIT Code) SaveGame/<SaveAsync>d__15:MoveNext ()

Can you share the code where you copy the entities between worlds? It’ll also be nice to see how you collect/instantiate the entities you copy.

Sure, i use my regular world to copy it to do an async save with the temporary created world. So i guess a regular usecase for this function :slight_smile:

var srcEM = World.DefaultGameObjectInjectionWorld.EntityManager;
World tmpWorld = new World("SaveGame" + tempWorldIndex++, WorldFlags.Shadow);
var tmpEM = tmpWorld.EntityManager;
var srcEntities = new EntityQueryBuilder(Allocator.Temp).WithNone<DontPersist>().WithOptions(EntityQueryOptions.IncludeDisabledEntities).Build(srcEM).ToEntityArray(Allocator.Temp);
var tmpEntities = new NativeArray<Entity>(srcEntities.Count(), Allocator.Persistent);

srcEM.CompleteAllTrackedJobs();
tmpEM.CopyEntitiesFrom(srcEM, srcEntities, tmpEntities);
...

It worked for several years now, just when updated to unity 6 it seems broken (in some rare cases). It happens after several hours of playing, and saving every few minutes like that.

Anyone from unity maybe who can give a hint what can be wrong?

It really looks, that unity give up trying to stabilize unity 6… I guess i need to stay on the old version and check for a new game engine to use for our next game…

There was another nullpointer exception deeper in the CopyEntitesFrom in my logs. Maybe can give a hint to the bug or another bug?

NullReferenceException: Object reference not set to an instance of an object
Unity.Entities.EntityComponentStore.RequiresBuildingResidueSharedComponentIndices (Unity.Entities.Archetype* srcArchetype, Unity.Entities.Archetype* dstArchetype) (at <f4e5b49c5fe844318e957a88011b5907>:0)
Unity.Entities.EntityComponentStore.InstantiateEntitiesOne (Unity.Entities.Entity srcEntity, Unity.Entities.Entity* outputEntities, System.Int32 instanceCount, Unity.Entities.EntityComponentStore+InstantiateRemapChunk* remapChunks, System.Int32 remapChunksCount, System.Boolean removePrefab) (at <f4e5b49c5fe844318e957a88011b5907>:0)
Unity.Entities.EntityComponentStore.InstantiateEntitiesGroup (Unity.Entities.Entity* srcEntities, System.Int32 srcEntityCount, Unity.Entities.Entity* outputRootEntities, System.Boolean outputRootEntityOnly, System.Int32 instanceCount, System.Boolean removePrefab) (at <f4e5b49c5fe844318e957a88011b5907>:0)
Unity.Entities.EntityComponentStore.InstantiateEntities (Unity.Entities.Entity* srcEntity, Unity.Entities.Entity* outputEntities, System.Int32 entityCount, System.Boolean removePrefab) (at <f4e5b49c5fe844318e957a88011b5907>:0)
Unity.Entities.StructuralChange+InstantiateEntities_00000FAE$BurstDirectCall.Invoke (Unity.Entities.EntityComponentStore* entityComponentStore, Unity.Entities.Entity* srcEntities, Unity.Entities.Entity* outputEntities, System.Int32 entityCount, System.Boolean removePrefab) (at <f4e5b49c5fe844318e957a88011b5907>:0)
Unity.Entities.StructuralChange.InstantiateEntities (Unity.Entities.EntityComponentStore* entityComponentStore, Unity.Entities.Entity* srcEntities, Unity.Entities.Entity* outputEntities, System.Int32 entityCount, System.Boolean removePrefab) (at <f4e5b49c5fe844318e957a88011b5907>:0)
Unity.Entities.EntityDataAccess.InstantiateInternalDuringStructuralChange (Unity.Entities.Entity* srcEntities, Unity.Entities.Entity* outputEntities, System.Int32 count, System.Int32 outputCount, System.Boolean removePrefab, Unity.Entities.SystemHandle& originSystem) (at <f4e5b49c5fe844318e957a88011b5907>:0)
Unity.Entities.EntityManager.CopyEntitiesInternal (Unity.Collections.NativeArray`1[T] srcEntities, Unity.Collections.NativeArray`1[T] outputEntities) (at <f4e5b49c5fe844318e957a88011b5907>:0)
Unity.Entities.EntityManager.CopyEntitiesFrom (Unity.Entities.EntityManager srcEntityManager, Unity.Collections.NativeArray`1[T] srcEntities, Unity.Collections.NativeArray`1[T] outputEntities) (at <f4e5b49c5fe844318e957a88011b5907>:0)
SaveGame+<SaveAsync>d__16.MoveNext () (at <6dbf22343f3c43cc829e00c9bebfc44f>:0)