Search Unity

Help Wanted Can't understand why I get and 'InvalidOperationException' error on Scene change

Discussion in 'Data Oriented Technology Stack' started by RBogdy, Apr 28, 2020.

  1. RBogdy

    RBogdy

    Joined:
    Mar 6, 2019
    Posts:
    60
    I have a simple system that deletes all entities and changes the scene and it used to work as expected however lately (if I trace it correctly is after the 0.3.0-preview.1 update) it keeps throwing this error:

    InvalidOperationException: The BlobAssetReference is not valid. Likely it has already been unloaded or released.
    Unity.Entities.BlobAssetReferenceData.ValidateNonBurst () (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/Blobs.cs:214)
    Unity.Entities.BlobAssetReferenceData.ValidateAllowNull () (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/Blobs.cs:240)
    Unity.Entities.BlobAssetReference`1[T].GetUnsafePtr () (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/Blobs.cs:285)
    Unity.Physics.ManifoldQueries.BodyBody (Unity.Physics.RigidBody& rigidBodyA, Unity.Physics.RigidBody& rigidBodyB, Unity.Physics.MotionVelocity& motionVelocityA, Unity.Physics.MotionVelocity& motionVelocityB, System.Single collisionTolerance, System.Single timeStep, Unity.Physics.BodyIndexPair pair, Unity.Collections.NativeStream+Writer& contactWriter) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Collision/Queries/Manifold.cs:48)
    Unity.Physics.NarrowPhase+ParallelCreateContactsJob.ExecuteImpl (Unity.Physics.PhysicsWorld& world, System.Single timeStep, Unity.Collections.NativeArray`1[T] dispatchPairs, System.Int32 dispatchPairReadOffset, System.Int32 numPairsToRead, Unity.Collections.NativeStream+Writer& contactWriter) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Dynamics/Simulation/Narrowphase.cs:111)
    Unity.Physics.NarrowPhase+ParallelCreateContactsJob.Execute (System.Int32 workItemIndex) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Dynamics/Simulation/Narrowphase.cs:78)
    Unity.Jobs.IJobParallelForDeferExtensions+JobParallelForDeferProducer`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.jobs@0.2.8-preview.3/Unity.Jobs/IJobParallelForDefer.cs:58)

    Code (CSharp):
    1. public class ClearEntities : SystemBase
    2. {
    3.     EntityCommandBufferSystem m_entityCommandBufferSystem;
    4.  
    5.     protected override void OnCreate()
    6.     {
    7.         m_entityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    8.     }
    9.  
    10.     protected override void OnUpdate()
    11.     {
    12.         //var entities = World.DefaultGameObjectInjectionWorld.EntityManager.GetAllEntities(Allocator.TempJob);
    13.         //World.DefaultGameObjectInjectionWorld.EntityManager.DestroyEntity(entities);
    14.         //entities.Dispose();
    15.  
    16.         EntityCommandBuffer commandBuffer = m_entityCommandBufferSystem.CreateCommandBuffer();
    17.  
    18.         Entities
    19.             .ForEach((Entity entity) =>
    20.             {
    21.                 commandBuffer.DestroyEntity(entity);
    22.             }).Schedule();
    23.  
    24.         m_entityCommandBufferSystem.AddJobHandleForProducer(Dependency);
    25.     }
    26. }

    Code (CSharp):
    1. public class SceneLoader : MonoBehaviour
    2. {
    3.     ClearEntities clearEntitiesSystem;
    4.     public void Start()
    5.     {
    6.         clearEntitiesSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<ClearEntities>();
    7.     }
    8.     public void ClearAllEntities()
    9.     {
    10.         clearEntitiesSystem.Update();
    11.     }
    12.  
    13.     public void LoadNextScene()
    14.     {
    15.         int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    16.         //Debug.Log(currentSceneIndex);
    17.         ClearAllEntities();
    18.         SceneManager.LoadScene(currentSceneIndex + 1);
    19.     }
    20.  
    21.  
    22.     public void LoadLastScene()
    23.     {
    24.         int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    25.         ClearAllEntities();
    26.         SceneManager.LoadScene(currentSceneIndex - 1);
    27.     }
    28. }

    Anyone knows what's going on here? I don't really get what I am doing wrong.

    Thanks!
     
    Last edited: Apr 28, 2020
  2. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    455
    Seems like your system runs in the middle of the physics step. Try scheduling it sometime after physics (UpdateAfter(ExportPhysicsWorld) or similar) to make sure you don't delete data right in the middle of it being accessed.
     
  3. RBogdy

    RBogdy

    Joined:
    Mar 6, 2019
    Posts:
    60
    I added the [UpdateAfter(typeof(ExportPhysicsWorld))] after the [DisableAutoCreation] tag and I get the same error
     
  4. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    455
    That gives you scheduling in the correct order. You also need to wait for ExportPhysicsWorld.FinalJobHandle to make sure all jobs are done.
     
  5. RBogdy

    RBogdy

    Joined:
    Mar 6, 2019
    Posts:
    60
    Did the following modifications:
    Code (CSharp):
    1. [DisableAutoCreation]
    2. [UpdateAfter(typeof(ExportPhysicsWorld))]
    3.  
    4. public class ClearEntities : SystemBase
    5. {
    6.     EntityCommandBufferSystem m_entityCommandBufferSystem;
    7.     ExportPhysicsWorld m_exportPhysicsWorld;
    8.    
    9.     protected override void OnCreate()
    10.     {
    11.         m_entityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    12.         m_exportPhysicsWorld = World.GetOrCreateSystem<ExportPhysicsWorld>();
    13.     }
    14.  
    15.     protected override void OnUpdate()
    16.     {
    17.         EntityCommandBuffer.Concurrent commandBuffer = m_entityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
    18.         Dependency = JobHandle.CombineDependencies(m_exportPhysicsWorld.FinalJobHandle, Dependency);
    19.  
    20.         Dependency = Entities
    21.             .ForEach((Entity entity, int nativeThreadIndex) =>
    22.             {
    23.                 commandBuffer.DestroyEntity(nativeThreadIndex, entity);
    24.             }).ScheduleParallel(Dependency);
    25.  
    26.         m_entityCommandBufferSystem.AddJobHandleForProducer(Dependency);      
    27.     }
    28. }

    InvalidOperationException: The BlobAssetReference is not valid. Likely it has already been unloaded or released.
    Unity.Entities.BlobAssetReferenceData.ValidateNonBurst () (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/Blobs.cs:214)
    Unity.Entities.BlobAssetReferenceData.ValidateAllowNull () (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/Blobs.cs:240)
    Unity.Entities.BlobAssetReference`1[T].GetUnsafePtr () (at Library/PackageCache/com.unity.entities@0.9.1-preview.15/Unity.Entities/Blobs.cs:285)
    Unity.Physics.ManifoldQueries.BodyBody (Unity.Physics.RigidBody& rigidBodyA, Unity.Physics.RigidBody& rigidBodyB, Unity.Physics.MotionVelocity& motionVelocityA, Unity.Physics.MotionVelocity& motionVelocityB, System.Single collisionTolerance, System.Single timeStep, Unity.Physics.BodyIndexPair pair, Unity.Collections.NativeStream+Writer& contactWriter) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Collision/Queries/Manifold.cs:48)
    Unity.Physics.NarrowPhase+ParallelCreateContactsJob.ExecuteImpl (Unity.Physics.PhysicsWorld& world, System.Single timeStep, Unity.Collections.NativeArray`1[T] dispatchPairs, System.Int32 dispatchPairReadOffset, System.Int32 numPairsToRead, Unity.Collections.NativeStream+Writer& contactWriter) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Dynamics/Simulation/Narrowphase.cs:111)
    Unity.Physics.NarrowPhase+ParallelCreateContactsJob.Execute (System.Int32 workItemIndex) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Dynamics/Simulation/Narrowphase.cs:78)
    Unity.Jobs.IJobParallelForDeferExtensions+JobParallelForDeferProducer`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.jobs@0.2.8-preview.3/Unity.Jobs/IJobParallelForDefer.cs:58)

    Also thank you for helping me with this :)
     
  6. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    455
    One final thing, can you add your job handle to the EndFramePhysicsSystem.HandlesToWaitFor? :) Also make sure you UpdateBefore(EndFramePhysicsSystem).
     
  7. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    455
    Also, you might want to take a look at the UnityPhysicsSamplesTest in the Samples project, looks like it's already doing what you are trying to do.
     
  8. RBogdy

    RBogdy

    Joined:
    Mar 6, 2019
    Posts:
    60
    Modified to:

    Code (CSharp):
    1. [DisableAutoCreation]
    2. [UpdateAfter(typeof(ExportPhysicsWorld)), UpdateBefore(typeof(EndFramePhysicsSystem))]
    3.  
    4. public class ClearEntities : SystemBase
    5. {
    6.     EntityCommandBufferSystem m_entityCommandBufferSystem;
    7.     ExportPhysicsWorld m_exportPhysicsWorld;
    8.     EndFramePhysicsSystem m_EndFramePhysicsSystem;
    9.  
    10.     protected override void OnCreate()
    11.     {
    12.         m_entityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    13.         m_exportPhysicsWorld = World.GetOrCreateSystem<ExportPhysicsWorld>();
    14.         m_EndFramePhysicsSystem = World.GetOrCreateSystem<EndFramePhysicsSystem>();
    15.     }
    16.  
    17.     protected override void OnUpdate()
    18.     {
    19.         EntityCommandBuffer.Concurrent commandBuffer = m_entityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
    20.        
    21.         Dependency = Entities
    22.             .ForEach((Entity entity, int nativeThreadIndex) =>
    23.             {
    24.                 commandBuffer.DestroyEntity(nativeThreadIndex, entity);
    25.             }).ScheduleParallel(JobHandle.CombineDependencies(m_exportPhysicsWorld.FinalJobHandle, Dependency));
    26.  
    27.         m_entityCommandBufferSystem.AddJobHandleForProducer(Dependency);
    28.         m_EndFramePhysicsSystem.HandlesToWaitFor.Add(Dependency);
    29.     }
    30. }

    Same error pops up.

    As for the samples ... it's a bit confusing. What I see different is that it disposes all systems as well ... however I don't get it why it used to work fine for me and now it's not working anymore...
     
  9. RBogdy

    RBogdy

    Joined:
    Mar 6, 2019
    Posts:
    60
    Modifying the Mono to the below setup seems to have solved the error. Used the code from the samples :) Thank you again!

    Code (CSharp):
    1. public class SceneLoader : MonoBehaviour
    2. {
    3.     ClearEntities clearEntitiesSystem;
    4.     public void Start()
    5.     {
    6.         clearEntitiesSystem = World.DefaultGameObjectInjectionWorld.GetOrCreateSystem<ClearEntities>();
    7.     }
    8.    
    9.  
    10.     public void LoadNextScene()
    11.     {
    12.         int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    13.        
    14.         clearEntitiesSystem.Update();
    15.         if (World.DefaultGameObjectInjectionWorld.IsCreated)
    16.         {
    17.             var systems = World.DefaultGameObjectInjectionWorld.Systems;
    18.             foreach (var s in systems)
    19.             {
    20.                 s.Enabled = false;
    21.             }
    22.             World.DefaultGameObjectInjectionWorld.Dispose();
    23.         }
    24.  
    25.         DefaultWorldInitialization.Initialize("Default World", false);
    26.         SceneManager.LoadScene(currentSceneIndex + 1);
    27.     }
    28.  
    29.  
    30.     public void LoadLastScene()
    31.     {
    32.         int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    33.         clearEntitiesSystem.Update();
    34.         SceneManager.LoadScene(currentSceneIndex - 1);
    35.     }
    36. }
     
    petarmHavok likes this.
  10. RBogdy

    RBogdy

    Joined:
    Mar 6, 2019
    Posts:
    60
    The issues reappeared after updating to latest Entities version. The strange thing is now if I have Burst enabled I get the error once and then it doesn't show anymore (i.e: Play -> change scene -> error -> exit Play mode -> Play -> no more error)

    If I turn Burst Compilation safety checks ... I get the error again. If I turn Burst off ... I get the error again.

    I wanted to create a post in the DOTS main forum maybe someone there knows what's going on. Can we have this moved?


    InvalidOperationException: The BlobAssetReference is not valid. Likely it has already been unloaded or released.
    Unity.Entities.BlobAssetReferenceData.ValidateNonBurst () (at Library/PackageCache/com.unity.entities@0.10.0-preview.6/Unity.Entities/Blobs.cs:213)
    Unity.Entities.BlobAssetReferenceData.ValidateAllowNull () (at Library/PackageCache/com.unity.entities@0.10.0-preview.6/Unity.Entities/Blobs.cs:238)
    Unity.Entities.BlobAssetReference`1[T].GetUnsafePtr () (at Library/PackageCache/com.unity.entities@0.10.0-preview.6/Unity.Entities/Blobs.cs:283)
    Unity.Physics.ManifoldQueries.BodyBody (Unity.Physics.RigidBody& rigidBodyA, Unity.Physics.RigidBody& rigidBodyB, Unity.Physics.MotionVelocity& motionVelocityA, Unity.Physics.MotionVelocity& motionVelocityB, System.Single collisionTolerance, System.Single timeStep, Unity.Physics.BodyIndexPair pair, Unity.Collections.NativeStream+Writer& contactWriter) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Collision/Queries/Manifold.cs:48)
    Unity.Physics.NarrowPhase+ParallelCreateContactsJob.ExecuteImpl (Unity.Physics.PhysicsWorld& world, System.Single timeStep, Unity.Collections.NativeArray`1[T] dispatchPairs, System.Int32 dispatchPairReadOffset, System.Int32 numPairsToRead, Unity.Collections.NativeStream+Writer& contactWriter) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Dynamics/Simulation/Narrowphase.cs:111)
    Unity.Physics.NarrowPhase+ParallelCreateContactsJob.Execute (System.Int32 workItemIndex) (at Library/PackageCache/com.unity.physics@0.3.2-preview/Unity.Physics/Dynamics/Simulation/Narrowphase.cs:78)
    Unity.Jobs.IJobParallelForDeferExtensions+JobParallelForDeferProducer`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at Library/PackageCache/com.unity.jobs@0.2.9-preview.15/Unity.Jobs/IJobParallelForDefer.cs:58)
     
    Adam-Mechtley likes this.
  11. RBogdy

    RBogdy

    Joined:
    Mar 6, 2019
    Posts:
    60
unityunity