Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Entitiy Heirarchy+Inspector Windows: EntityManager has been deallocated

Discussion in 'Entity Component System' started by CynicalBusiness, Jun 11, 2023.

  1. CynicalBusiness

    CynicalBusiness

    Joined:
    May 3, 2020
    Posts:
    10
    I recently updated to Entities 1.0.10 (up from pre.65) on a small PoC project, and I'm noticing more and more the editor tooling spatting an error into the console:
    ObjectDisposedException: The Unity.Entities.EntityManager has been deallocated, it is not allowed to access it


    This seems to occur when exiting play mode while an entity is selected in the hierarchy and its inspector is visible. Even worse still, if that entity features a DynamicBuffer of some kind, the error is flooded in the console ~100-200 times per second to a point where the few minutes I stepped away while it was doing that left me with a few GB of an editor log file upon return.


    This is my console when leaving play mode. Note the error count in the upper right.

    If it helps any, here's the offending entity's archetype:

    That's one enablable tag, three DynamicBuffers, two shared components, one managed data component (Prototype, doesn't get its own icon), and 8 regular-old unmanaged components. If any of those component's contents are relevant I can share them at request.

    The "workaround," it seems, is just to start and stop play mode again without touching any entities or to make sure that I have a GameObject rather than an entity selected in the hierarchy.

    Full stack trace:
    Code (csharp):
    1. ObjectDisposedException: The Unity.Entities.EntityManager has been deallocated, it is not allowed to access it
    2. Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckWriteAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <7ab1866afe2743b3ab3c564ec1223b70>:0)
    3. Unity.Entities.EntityManager.GetCheckedEntityDataAccess () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/EntityManager.cs:86)
    4. Unity.Entities.EntityManager.get_EntityCapacity () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/EntityManager.cs:183)
    5. Unity.Entities.Editor.EntityManagerEditorExtensions.SafeExists (Unity.Entities.EntityManager entityManager, Unity.Entities.Entity entity) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.Editor/Extensions/EntityManagerEditorExtensions.cs:7)
    6. Unity.Entities.EntityContainer.Exists () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.Editor/Inspector/EntityContainer.cs:57)
    7. Unity.Entities.EntityContainerPropertyBag+<EnumerateProperties>d__14.MoveNext () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.Editor/Inspector/EntityContainer.cs:318)
    8. Unity.Properties.PropertyCollection`1+Enumerator[TContainer].MoveNext () (at <18a56243cae44610b90ff35d6bbdbdd9>:0)
    9. Unity.Entities.Editor.ComponentsTab+ComponentsTabInspector.BuildOrUpdateUI () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.Editor/Inspector/ComponentsTab.cs:91)
    10. Unity.Entities.Editor.ComponentsTab+ComponentsTabInspector.Update () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.Editor/Inspector/ComponentsTab.cs:80)
    11. Unity.Entities.UI.CustomInspectorElement.UnityEngine.UIElements.IBinding.Update () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.UI.Editor/Elements/Internal/CustomInspectorElement.cs:66)
    12. UnityEngine.Debug:LogException(Exception)
    13. Unity.Entities.UI.CustomInspectorElement:UnityEngine.UIElements.IBinding.Update() (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities.UI.Editor/Elements/Internal/CustomInspectorElement.cs:70)
    14. UnityEditor.RetainedMode:UpdateSchedulers()
    Edit:
    Editor version is 2022.2.20
     
    Last edited: Jun 11, 2023
    One1Guy and PolarTron like this.
  2. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    106
    I'm running into the same issue. Your proposed solution isn't working for me. The only thing I can do is restart the Editor. I am also interested in getting this fixed, because the Editor windows for ECS is essential to debugging.
     
  3. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,623
    Honestly totally forgot this was a thing because I have a workaround for it but it must have been a problem for 8? months now. I simply have a system in editor on play mode change it unselects the entity hierarchy

    Code (CSharp):
    1. [InitializeOnLoad]
    2. public static class EditorWorldSafeShutdown
    3. {
    4.     static EditorWorldSafeShutdown()
    5.     {
    6.         EditorApplication.playModeStateChanged += _ => EntitySelection.UnSelect();
    7.     }
    8. }
    9.  
    10. public static class EntitySelection
    11. {
    12.     public static void UnSelect()
    13.     {
    14.         if (Selection.activeObject is EntitySelectionProxy)
    15.         {
    16.             Selection.activeObject = null;
    17.         }
    18.     }
    19. }
    (note this requires internal access, you could just always set Selection.activeObject = null if you don't want to get internal access but could be annoying in certain situations.)
     
    Last edited: Jun 11, 2023
    Selmar and DatCong like this.
  4. BenjaminApprill

    BenjaminApprill

    Joined:
    Aug 1, 2022
    Posts:
    106
    Ok. The original post seems to be working now... I need to clear the entity selection, AND make sure the entity isn't being displayed in the Inspector. I'm no longer getting the error if I make sure to do that.
     
  5. CynicalBusiness

    CynicalBusiness

    Joined:
    May 3, 2020
    Posts:
    10
    There's something distinctly Unity about automating a work-around for an editor bug. :D

    I made some minor tweaks to your script to fit my case and avoid internal access, but generally this is a great idea. Thanks!

    Here's the modified script for reference, in case anyone wants it.
    Code (CSharp):
    1. [InitializeOnLoad]
    2. public static class EntitySelectionFix
    3. {
    4.     static EntitySelectionFix()
    5.     {
    6.         EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
    7.     }
    8.  
    9.     private static void OnPlayModeStateChanged(PlayModeStateChange state)
    10.     {
    11.         if (state == PlayModeStateChange.ExitingPlayMode
    12.             && Selection.activeObject != null
    13.             && Selection.activeObject.GetType().Name == "EntitySelectionProxy")
    14.         {
    15.             Selection.activeObject = null;
    16.         }
    17.     }
    18. }
     
  6. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,623
    Just a heads up, this bug can happen in edit mode entering play mode if you have an entity selected.
     
  7. CynicalBusiness

    CynicalBusiness

    Joined:
    May 3, 2020
    Posts:
    10
    Can you select an entity in edit mode? I've yet to see any way to do that.

    Then again, I don't use the baker/authoring approach to making entities, so my hierarchy is always in the "GameObjects only" mode when not playing. That can cause it too?
     
  8. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,623
    Well if you don't use baking then no (but you really should...)
     
  9. CynicalBusiness

    CynicalBusiness

    Joined:
    May 3, 2020
    Posts:
    10
    Baking just didn't meet the requirements of the project (entity prefabs needed to be very dynamic and changeable by assemblies outside the editor) so a different system was devised.

    But that's beside the point; indeed if I switch to Runtime mode and play while an entity is selected, I also get this error (or a flood of errors for DynamicBuffers). Updated the script to not check for that state. Cheers!
     
  10. gurth

    gurth

    Joined:
    Sep 21, 2014
    Posts:
    6
    Same issue here. I'm looking at HelloCube demo. Opening every example scene and subscene I noticed that I have got ISystems form previously opened examples. Maybe its something with ISystem unloading or maybe there is some Isystem cache not being cleared after scene change?
    upload_2023-8-4_19-7-2.png