Search Unity

Bug EntityManager.CopyEntitiesFrom with Temp memory leads to InvalidOperationException

Discussion in 'Entity Component System' started by sunrisebacon, Jun 21, 2020.

  1. sunrisebacon

    sunrisebacon

    Joined:
    Jul 15, 2017
    Posts:
    16
    I'm using EntityManager.CopyEntitiesFrom inside a system (but not a job) to copy entities to other worlds. If I copy more than a handful of entities I get this exception:

    Code (CSharp):
    1. InvalidOperationException: The Unity.Collections.NativeArray`1[Unity.Entities.Entity] GatherEntityInChunkForEntities.Entities is allocated with Temp memory. Temp memory containers cannot be used when scheduling a job, use TempJob instead.
    2. Unity.Jobs.LowLevel.Unsafe.JobsUtility.ScheduleParallelFor (Unity.Jobs.LowLevel.Unsafe.JobsUtility+JobScheduleParameters& parameters, System.Int32 arrayLength, System.Int32 innerloopBatchCount) (at <fdd4f5823e2a41e8be8d5dcbd0bfd5b1>:0)
    3. Unity.Jobs.IJobParallelForExtensions.Schedule[T] (T jobData, System.Int32 arrayLength, System.Int32 innerloopBatchCount, Unity.Jobs.JobHandle dependsOn) (at <fdd4f5823e2a41e8be8d5dcbd0bfd5b1>:0)
    4. Unity.Entities.EntityComponentStore.GatherEntityInChunkForEntitiesJob (Unity.Collections.NativeArray`1[T] entities, Unity.Collections.NativeArray`1[T] entityChunkData, Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.entities@0.11.1-preview.4/Unity.Entities/EntityComponentStore.cs:1111)
    5. ...
    My guess is that after I reach a certain number of entities the operation takes longer than one frame which leads to the exception.
    I suspect line 22 inside EntityManagerCopyEntitiesFrom.cs is at fault:
    Code (CSharp):
    1. ...
    2.     using (var srcManagerInstances = new NativeArray<Entity>(srcEntities.Length, Allocator.Temp))
    3. ...
    I think TempJob memory should be used instead (same as a few lines further down).
     
  2. sunrisebacon

    sunrisebacon

    Joined:
    Jul 15, 2017
    Posts:
    16
    I can't edit my post so I post a correction here:
    I don't think the frame is the problem as the code should run on the main thread.
    I don't know if the error message is wrong or if an implicit job is created because I use an EntityQuery to collect the entities I want to copy.
     
    cvoigt likes this.
  3. cvoigt

    cvoigt

    Joined:
    Dec 29, 2013
    Posts:
    9
    Hi, I had the same problem in April and created an issue here that was closed, because I did not add a minimal repro project (which I thought was unnecessary as I clearly described what line of code was causing the problem). Let's hope that you are more successful in raising this issue.
     
    sunrisebacon likes this.
  4. s_schoener

    s_schoener

    Unity Technologies

    Joined:
    Nov 4, 2019
    Posts:
    81
    cvoigt and MNNoxMortem like this.