diff --git a/source/DefaultEcs.Test/EntitySortedSetTest.cs b/source/DefaultEcs.Test/EntitySortedSetTest.cs index f95a9dae..c9e007d4 100644 --- a/source/DefaultEcs.Test/EntitySortedSetTest.cs +++ b/source/DefaultEcs.Test/EntitySortedSetTest.cs @@ -275,6 +275,19 @@ public void EntityRemoved_Should_be_called() Check.That(entity).IsEqualTo(removedEntity); } + [Fact] + public void Remove_While_Full_Should_Not_Crash() + { + using World world = new(); + + for (int i = 0; i < 8; i++) // choose count such that _entities is completly used + world.CreateEntity().Set(i); + + using EntitySortedSet set = world.GetEntities().AsSortedSet(); + + set.GetEntities()[0].Remove(); + } + #endregion } } diff --git a/source/DefaultEcs/EntitySortedSet.cs b/source/DefaultEcs/EntitySortedSet.cs index 042d5c97..c2dae4e7 100644 --- a/source/DefaultEcs/EntitySortedSet.cs +++ b/source/DefaultEcs/EntitySortedSet.cs @@ -176,7 +176,7 @@ void Internal.IEntityContainer.Remove(int entityId) ref int index = ref _mapping[entityId]; if (index != -1) { - int length = Count-- - index; + int length = --Count - index; if (length > 0) {