#NullreferenceException when using iterator on SystepAPI.Query

1 messages · Page 1 of 1 (latest)

fallow mulch
#

Hello, I have a crash while using DOTS that I can't really figure out by myself. A little help would be really appreciated.

  at Unity.Entities.ArchetypeChunk.get_Count () [0x00000] in C:\Users\gross\Documents\Code\lospinguinos_unity\deps\com.unity.entities\Unity.Entities\Iterators\ArchetypeChunkArray.cs:29 
  at Unity.Entities.EntityQueryEnumerator.MoveNextEntityRange (System.Boolean& movedToNewChunk, Unity.Entities.ArchetypeChunk& chunk, System.Int32& entityStartIndex, System.Int32& entityEndIndex) [0x00095] in C:\Users\gross\Documents\Code\lospinguinos_unity\deps\com.unity.entities\Unity.Entities\Iterators\EntityQueryEnumerator.cs:175 ```
Really rarely I get this exception when trying to iterate over a query, in a bursted system. The query is nothing to fancy :
```            foreach (var (grabbable, _, physicsCollider, e) in SystemAPI.Query<RefRW<Grabbable>, RefRW<PhysicsVelocity>, RefRW<PhysicsCollider>>().WithEntityAccess())
            {```
or 
```            foreach (var (grabbable, localTransform, physicCollider, e) in SystemAPI.Query<RefRW<Grabbable>, RefRW<LocalTransform>, RefRW<PhysicsCollider>>()
                .WithNone<PhysicsVelocity>()
                .WithEntityAccess())```
I don't really know which one caused the crash.

It looks like the chunk returned by the iterator is null, but I don't see why. 
Does somebody experienced it, or have any clue on what chould have happenned ?
ebon shuttle
#

SystemAPI can only be called within systems

#

and you mustn't store it in a variable, because it's not actual iterator, but just a flag for codegen

fallow mulch
#

Of course, this forloops are called in a system. And there is no storing in variables in here.

novel nexus
#

show the full update

#

i suspect you have a structural change in there or something?

fallow mulch
#

I couldn't see any, but I might have missed something of course

novel nexus
#

hmm they look fine

ebon shuttle
#

at what line error points?

fallow mulch
#

I don't have the line exactly. This is when advancing in the iterator of one of the foreach loops, while accessing the chunk to iterate over

novel nexus
#

you might be able to see what loop it is in if you look at the code gen from the stack?

ebon shuttle
#

full error likely contains line

fallow mulch
#

So it might actually be the second loop

novel nexus
#

my only thought is maybe something like
localTransform.ValueRW = state.EntityManager.GetComponentData<LocalTransform>(grabbable.ValueRO.GrabbingEntity);

#

invalidating
RefRW<LocalTransform>

#

in the loop

#

if you have a repo i'd try replace that with an ecb.setcomponent otherwise i'm really not seeing much else

#

oh wait no

#

im completely wrong

astral hinge
novel nexus
#

ignore that

#

i thought it was getting transform and writing to it

#

but its reading transform and writing to one in the loop

ebon shuttle
#

maybe use SystemAPI.GetComponent here?

#

it's more efficient at very least

novel nexus
#

is it?

ebon shuttle
#

it is using lookup

#

vs entity manager

novel nexus
#

why is that more efficient?

ebon shuttle
#

and it's sync point is codegened

#

vs inlined into method call

novel nexus
#

i guess it doesn't have to dependency lookup each call

ebon shuttle
#
        public T GetComponentData<T>(Entity entity) where T : unmanaged, IComponentData
        {
            var typeIndex = TypeManager.GetTypeIndex<T>();

            EntityComponentStore->AssertEntityHasComponent(entity, typeIndex);
            EntityComponentStore->AssertNotZeroSizedComponent(typeIndex);

            if (!IsInExclusiveTransaction)
                DependencyManager->CompleteWriteDependency(typeIndex);

            var ptr = EntityComponentStore->GetComponentDataWithTypeRO(entity, typeIndex);

            T value;
            UnsafeUtility.CopyPtrToStructure(ptr, out value);
            return value;
        }
#

hmm

novel nexus
#

DependencyManager->CompleteWriteDependency(typeIndex);
just ditches this line i'd assume

ebon shuttle
#

well yeah

#

but it's a somewhat heavy line

novel nexus
#

yeah makes sense, never really considered it

#

(i rarely do anything on main thread but going to keep this in mind)

astral hinge
#

its grasping at straws but changing the code could change its behavior 🤷

novel nexus
#

might need to see what it codegenned into

ebon shuttle
novel nexus
#

i guess one downside is if you dnot actually end up using it

#

you still have the complete if you dont need it

#

but odds are you are going to have a complete anyway due to doing stuff on mainthread

fallow mulch
#

That's interesting ! Not sure of how it would affect the chunk/iterator behavior though

ebon shuttle
#

@fallow mulch disable burst if it was enabled

#

error should be far more clear

fallow mulch
#

I don't have a repro really. It only happen once in a while. Also, it only happened in a build for the moment.
I'll play with it unbursted for a while

fallow mulch
#

Actually, the crash is caused by that. The nullreference exception is sth that happenned just before.

0x00007FFBFB1E8290 (lib_burst_generated) [C:\Users\gross\Documents\Code\lospinguinos_unity\deps\com.unity.entities\Unity.Entities\ChunkDataUtility.cs:1246] Unity.Entities.ChunkDataUtility.Deallocate 
0x00007FFBFB1EE9E2 (lib_burst_generated) [C:\Users\gross\Documents\Code\lospinguinos_unity\deps\com.unity.entities\Unity.Entities\EntityComponentStoreCreateDestroyEntities.cs:72] Unity.Entities.EntityComponentStore.DestroyEntities 
0x00007FFBFB205D8D (lib_burst_generated) [C:\Users\gross\Documents\Code\lospinguinos_unity\deps\com.unity.entities\Unity.Entities\EntityDataAccess.cs:611] Unity.Entities.EntityDataAccess.DestroyEntityInternalDuringStructuralChange 
0x00007FFBFB2911B2 (lib_burst_generated) [C:\Users\gross\Documents\Code\lospinguinos_unity\deps\com.unity.entities\Unity.Entities\EntityCommandBuffer.cs:3514] Unity.Entities.EntityCommandBuffer.EcbWalker`1<Unity.Entities.EntityCommandBuffer.PlaybackProcessor>.ProcessChain ```
#

And I got this exact same crash without the exception before...

novel nexus
#

looks like destroy entity is being called multiple times on the same entity

fallow mulch
#

This might be a good guess. We found an entity that might have been destroyed twice. Even though it did not look like this triggered in the frames near the crash, we will see if that happens again.