Cleaning up Entities from MonoBehaviours

I am still quite new to ECS, I have managed to setup a basic stats system with it that works quite well for my needs, but now I seem to be having a problem calling DestroyEntity. I am creating and destroying my entities from MonoBehaviour “Manager” scripts. It works just fine in normal gameplay, but (often) throws a lot of quite nasty looking errors in the console once I exit play mode, and I’m worried that could be part of a much bigger problem. Here is my code and it is the destroy entity part (in OnDestroy) that is causing the problem.

public class MonsterStatsManager : MonoBehaviour
{
    public MonsterStats Stats => entity.Index > 0 ? entityManager.GetComponentData<MonsterStats>(entity) : monsterStats;

    [SerializeField] MonsterStats monsterStats;

    Entity entity;
    static EntityManager entityManager;
    static EntityArchetype monsterArchetype;

    void Awake()
    {
        entityManager = entityManager == default(EntityManager) ? World.DefaultGameObjectInjectionWorld.EntityManager : entityManager;
        monsterArchetype = monsterArchetype == default(EntityArchetype) ? entityManager.CreateArchetype(typeof(MonsterStats), typeof(MonsterStatsDispatcher)) : monsterArchetype;
    }

    void Start()
    {
        entity = entityManager.CreateEntity(monsterArchetype);
        monsterStats.name = Regex.Replace(gameObject.name, @" ?\(.*?\)", string.Empty).Trim();
        entityManager.SetName(entity, $"MonsterStats ({gameObject.name})");
        entityManager.SetComponentData(entity, monsterStats);
    }

    void OnDestroy() => entityManager.DestroyEntity(entity);

    public void Dispatch<T>(T dispatch) where T : struct => StatsDispatchHelpers.Dispatch<T, MonsterStatsDispatcher>(dispatch, entity);
}

The error I am getting is this and sometimes multiple times, happens only when stopping play mode, and sometimes doesn’t happen:

Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckWriteAndThrowNoEarlyOut (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <2feaf16e80004e0cadae3f2e05f2a3fa>:0)
Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle.CheckWriteAndThrow (Unity.Collections.LowLevel.Unsafe.AtomicSafetyHandle handle) (at <2feaf16e80004e0cadae3f2e05f2a3fa>:0)
Unity.Entities.EntityManager.GetCheckedEntityDataAccess () (at Library/PackageCache/com.unity.entities@0.11.2-preview.1/Unity.Entities/EntityManager.cs:70)
Unity.Entities.EntityManager.DestroyEntityInternal (Unity.Entities.Entity* entities, System.Int32 count) (at Library/PackageCache/com.unity.entities@0.11.2-preview.1/Unity.Entities/EntityManagerCreateDestroyEntities.cs:341)
Unity.Entities.EntityManager.DestroyEntity (Unity.Entities.Entity entity) (at Library/PackageCache/com.unity.entities@0.11.2-preview.1/Unity.Entities/EntityManagerCreateDestroyEntities.cs:172)
PlayerStatsManager.OnDestroy () (at Assets/Scripts/Stats/PlayerStatsManager.cs:34```

Think I figured out this one myself, it was my cached EntityManager that was causing the problem, I reworked it a bit to check if the world was null (and also read EntityManager directly from World.DefaultGameObjectInjectionWorld).

public class MonsterStatsManager : MonoBehaviour
{
    public MonsterStats Stats => entity.Index > 0 ? World.EntityManager.GetComponentData<MonsterStats>(entity) : monsterStats;

    [SerializeField] MonsterStats monsterStats;

    Entity entity;
    static World World => World.DefaultGameObjectInjectionWorld;
    static EntityArchetype monsterArchetype;

    void Awake()
    {
        monsterArchetype = monsterArchetype == default(EntityArchetype) ? World.EntityManager.CreateArchetype(typeof(MonsterStats), typeof(MonsterStatsDispatcher)) : monsterArchetype;
    }

    void Start()
    {
        EntityManager entityManager = World.EntityManager;
        entity = entityManager.CreateEntity(monsterArchetype);
        monsterStats.name = Regex.Replace(gameObject.name, @" ?\(.*?\)", string.Empty).Trim();
        entityManager.SetName(entity, $"MonsterStats ({gameObject.name})");
        entityManager.SetComponentData(entity, monsterStats);
    }

    void OnDestroy() => World?.EntityManager.DestroyEntity(entity);

    public void Dispatch<T>(T dispatch) where T : struct => StatsDispatchHelpers.Dispatch<T, MonsterStatsDispatcher>(dispatch, entity);
}