Search Unity

ECS Physics Error: native array has been deallocated

Discussion in 'Entity Component System' started by jParmentier, Jun 27, 2019.

  1. jParmentier

    jParmentier

    Joined:
    Dec 4, 2017
    Posts:
    2
    Hi people.
    I am having some problem using the new unity ECS physics. I am wondering what I am doing wrong.

    This is the error I am having (in unity 2019.1.8f1, with Unity Physics preview - 0.1.0, unity collection preview.20 - 0.0.9 ):
    It happen inside :
    Code (CSharp):
    1.  
    2.                 Simulation simulation = new Simulation();
    3.                 SimulationStepInput stepInput = new SimulationStepInput
    4.                 {
    5.                     World                     = localWorld,
    6.                     TimeStep                  = 1/60f,
    7.                     ThreadCountHint           = Unity.Physics.PhysicsStep.Default.ThreadCountHint,
    8.                     Gravity                   = math.up() * -9.81f,
    9.                     SynchronizeCollisionWorld = true
    10.                 };
    11.  
    12.                 localWorld.CollisionWorld
    13.                           .ScheduleUpdateDynamicLayer(ref localWorld, stepInput.TimeStep, stepInput.ThreadCountHint,
    14.                               new JobHandle()).Complete();
    15.  
    Code (CSharp):
    1.  
    2. InvalidOperationException: The NativeArray has been deallocated, it is not allowed to access it
    3. Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckDeallocateAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) <0x2cc064c8240 + 0x00052> in <c0ef81e1639b423ab35ce0e4c08b5b7a>:0
    4. Unity.Collections.LowLevel.Unsafe.DisposeSentinel.Dispose (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle& safety, Unity.Collections.LowLevel.Unsafe.DisposeSentinel& sentinel) (at C:/buildslave/unity/build/Runtime/Export/NativeArray/DisposeSentinel.cs:69)
    5. Unity.Collections.NativeArray`1[T].Dispose () (at C:/buildslave/unity/build/Runtime/Export/NativeArray/NativeArray.cs:160)
    6. Unity.Physics.Broadphase+Tree.set_NodeCount (System.Int32 value) (at Library/PackageCache/com.unity.physics@0.1.0-preview/Unity.Physics/Collision/World/Broadphase.cs:259)
    7. Unity.Physics.Broadphase.ScheduleStaticTreeBuildJobs (Unity.Physics.PhysicsWorld& world, System.Int32 numThreadsHint, Unity.Physics.StaticLayerChangeInfo& staticLayerChangeInfo, Unity.Collections.NativeArray`1[T] previousFrameBodyFilters, Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.physics@0.1.0-preview/Unity.Physics/Collision/World/Broadphase.cs:69)
    8. Unity.Physics.Broadphase.ScheduleBuildJobs (Unity.Physics.PhysicsWorld& world, System.Single timeStep, System.Int32 numThreadsHint, Unity.Physics.StaticLayerChangeInfo& staticLayerChangeInfo, Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.physics@0.1.0-preview/Unity.Physics/Collision/World/Broadphase.cs:160)
    9. Unity.Physics.CollisionWorld.ScheduleUpdateDynamicLayer (Unity.Physics.PhysicsWorld& world, System.Single timeStep, System.Int32 numThreadsHint, Unity.Jobs.JobHandle inputDeps) (at Library/PackageCache/com.unity.physics@0.1.0-preview/Unity.Physics/Collision/World/CollisionWorld.cs:87)
    10. Apollo.PredictionReconSystem.ReconJob (Unity.Entities.EntityQuery query) (at Assets/Code/ECS/PredictionReconSystem.cs:155)
    11. Apollo.PredictionReconSystem.OnUpdate (Unity.Jobs.JobHandle inputDependencies) (at Assets/Code/ECS/PredictionReconSystem.cs:62)
    12. Unity.Entities.JobComponentSystem.InternalUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.33/Unity.Entities/ComponentSystem.cs:951)
    13. Unity.Entities.ComponentSystemBase.Update () (at Library/PackageCache/com.unity.entities@0.0.12-preview.33/Unity.Entities/ComponentSystem.cs:287)
    14. Unity.Entities.ComponentSystemGroup.OnUpdate () (at Library/PackageCache/com.unity.entities@0.0.12-preview.33/Unity.Entities/ComponentSystemGroup.cs:451)
    15. UnityEngine.Debug:LogException(Exception)
    16. Unity.Debug:LogException(Exception) (at Library/PackageCache/com.unity.entities@0.0.12-preview.33/Unity.Entities/Stubs/Unity/Debug.cs:25)
    17. Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library/PackageCache/com.unity.entities@0.0.12-preview.33/Unity.Entities/ComponentSystemGroup.cs:455)
    18.  
    So what I am trying to do is basically a variation on what the unity physics example "Immediate Mode".

    I want to create a second world ("shadow world"), which will be a low rez copy of my real world, and simulate physics on that world multiple step per frame. Then grab the result and put that in my "real" world.

    This system update in the default active world, but ask for simulation of the second world.

    UpdateStaticWorld will update on all entities of the real world, and add their physics component in the shadow world, then remove the tag component so that I don't add them twice.

    After that for each element that I need to process, I want to create them, put them in the physics world, process them and then delete them ( so they don't interfere with each other)
    Code (CSharp):
    1.  
    2. using Unity.Collections;
    3. using Unity.Entities;
    4. using Unity.Jobs;
    5. using Unity.Transforms;
    6. using Unity.Mathematics;
    7. using Unity.Physics;
    8. using Unity.Physics.Systems;
    9.  
    10. namespace Apollo
    11. {
    12.     public class PredictionReconSystem : JobComponentSystem
    13.     {
    14.         private World _shadowWorld;
    15.        
    16.         private EntityQuery _queryStaticObject;
    17.         private EntityQuery _queryAllNetworkedEntityObject;
    18.        
    19.         private NativeArray<CommonState> _currentStatesGameCube;
    20.         private uint                     _currentFrame;
    21.        
    22.        
    23.         protected override void OnCreate()
    24.         {
    25.             _shadowWorld = new World("shadowWorld");
    26.  
    27.             _shadowWorld.CreateSystem<BuildPhysicsWorld>();
    28.             _shadowWorld.GetOrCreateSystem<StepPhysicsWorld>();
    29.             _shadowWorld.GetOrCreateSystem<EndFramePhysicsSystem>();
    30.             _queryStaticObject = GetEntityQuery(ComponentType.ReadOnly<PhysicsCollider>(),
    31.                                                     ComponentType.ReadOnly<ShadowWorldTag>());
    32.                                                                                                                            
    33.             _queryAllNetworkedEntityObject = GetEntityQuery(ComponentType.ReadOnly<CommonState>(),
    34.                                                             ComponentType.ReadOnly<Translation>(),
    35.                                                             ComponentType.ReadOnly<Rotation>(),
    36.                                                             ComponentType.ReadOnly<CarState>(),
    37.                                                             ComponentType.ReadOnly<PhysicsVelocity>(),
    38.                                                             ComponentType.ReadOnly<PhysicsMass>(),
    39.                                                             ComponentType.ReadOnly<PhysicsCollider>(),
    40.                                                             ComponentType.ReadOnly<LocalToWorld>(),
    41.                                                             ComponentType.ReadOnly<PhysicsDamping>(),
    42.                                                             ComponentType.Exclude<OwnerState>());
    43.         }
    44.  
    45.         protected override JobHandle OnUpdate(JobHandle inputDependencies)
    46.         {
    47.             UpdateStaticWorld( _queryStaticObject);
    48.             ReconJob(_queryAllNetworkedEntityObject);
    49.  
    50.             return inputDependencies;
    51.         }
    52.    
    53.        
    54.         void UpdateStaticWorld( EntityQuery query)
    55.         {
    56.             NativeArray<Entity> entities          = query.ToEntityArray(Allocator.TempJob);
    57.             NativeArray<PhysicsCollider> collider = query.ToComponentDataArray<PhysicsCollider>(Allocator.TempJob);
    58.            
    59.             for (int i = 0; i < entities.Length; i++)
    60.             {
    61.                 PhysicsCollider c  = collider[i];
    62.                 Entity e = _shadowWorld.EntityManager.CreateEntity();
    63.                 _shadowWorld.EntityManager.AddComponentData(e, c);
    64.                 World.Active.EntityManager.RemoveComponent(entities[i], ComponentType.ReadWrite<ShadowWorldTag>());
    65.             }
    66.  
    67.             entities.Dispose();
    68.             collider.Dispose();
    69.         }
    70.  
    71.  
    72.         void ReconJob(EntityQuery query)
    73.         {
    74.             NativeArray<CarState>        CarsStates = query.ToComponentDataArray<CarState>(Allocator.TempJob);
    75.             NativeArray<Translation>     t          = query.ToComponentDataArray<Translation>(Allocator.TempJob);
    76.             NativeArray<Rotation>        r          = query.ToComponentDataArray<Rotation>(Allocator.TempJob);
    77.             NativeArray<PhysicsVelocity> pv         = query.ToComponentDataArray<PhysicsVelocity>(Allocator.TempJob);
    78.             NativeArray<PhysicsMass>     pm         = query.ToComponentDataArray<PhysicsMass>(Allocator.TempJob);
    79.             NativeArray<PhysicsCollider> pc         = query.ToComponentDataArray<PhysicsCollider>(Allocator.TempJob);
    80.             NativeArray<CommonState>     cs         = query.ToComponentDataArray<CommonState>(Allocator.TempJob);
    81.             NativeArray<LocalToWorld>    lw         = query.ToComponentDataArray<LocalToWorld>(Allocator.TempJob);
    82.             NativeArray<PhysicsDamping>  pd         = query.ToComponentDataArray<PhysicsDamping>(Allocator.TempJob);
    83.  
    84.             for (int i = 0; i < CarsStates.Length; i++)
    85.             {
    86.                
    87.                 Entity e = _shadowWorld.EntityManager.CreateEntity();
    88.                
    89.                 _shadowWorld.EntityManager.AddComponentData(e, CarsStates[i]);
    90.                 _shadowWorld.EntityManager.AddComponentData(e, t[i]);
    91.                 _shadowWorld.EntityManager.AddComponentData(e, r[i]);
    92.                 _shadowWorld.EntityManager.AddComponentData(e, pv[i]);
    93.                 _shadowWorld.EntityManager.AddComponentData(e, pm[i]);
    94.                 _shadowWorld.EntityManager.AddComponentData(e, pc[i]);
    95.                 _shadowWorld.EntityManager.AddComponentData(e, cs[i]);
    96.                 _shadowWorld.EntityManager.AddComponentData(e, lw[i]);
    97.                 _shadowWorld.EntityManager.AddComponentData(e, pd[i]);
    98.                 BuildPhysicsWorld bpw = _shadowWorld.GetExistingSystem<BuildPhysicsWorld>();
    99.  
    100.                 PhysicsWorld localWorld = bpw.PhysicsWorld;
    101.                
    102.                 Simulation simulation = new Simulation();
    103.                 SimulationStepInput stepInput = new SimulationStepInput
    104.                 {
    105.                     World                     = localWorld,
    106.                     TimeStep                  = 1/60f,
    107.                     ThreadCountHint           = PhysicsStep.Default.ThreadCountHint,
    108.                     Gravity                   = math.up() * -9.81f,
    109.                     SynchronizeCollisionWorld = true
    110.                 };
    111.  
    112.                 localWorld.CollisionWorld
    113.                           .ScheduleUpdateDynamicLayer(ref localWorld, stepInput.TimeStep, stepInput.ThreadCountHint,
    114.                               new JobHandle()).Complete();
    115.  
    116.                 for (int j = 0; j < 3; j++)
    117.                 {
    118.                     simulation.Step(stepInput);
    119.                 }
    120.  
    121.                //TODO: grab resulting position of entity
    122.                
    123.                 CarsStates.Dispose();
    124.                 t         .Dispose();
    125.                 r         .Dispose();
    126.                 pv        .Dispose();
    127.                 pm        .Dispose();
    128.                 pc        .Dispose();
    129.                 cs        .Dispose();
    130.                 lw        .Dispose();
    131.                 pd        .Dispose();
    132.                 _shadowWorld.EntityManager.DestroyEntity(e);
    133.             }
    134.            
    135.         }
    136.     }
    137. }
    138.  
    I really don t understand what I am doing wrong.
    Is my idea flawed ?
    Have I missed the creation of some systems on my shadow world ?
    Is that a bug inside unity ?
     
    LevonVH and Orion_Reed like this.