Orphaned SharedComponentData

I was trying to iterate over all my grids via

EntityManager.GetAllUniqueSharedComponentData(this.grids);

when I found out that there are 2 unique grids existing.

The grid component that I create intentionally by

this.gridComponentData = new GridComponent()
{
  Size = 3,
  CellSwizzle = this.Grid.CellSwizzle,
  Root = this.gridRootEntity
};
Manager.SetSharedComponentData(cellEntity, this.gridComponentData);

…and another grid which is created with default values (e.g. Size = 0).

I tried Logging all existing grids and how many entities are referencing them:

this.grids.Clear();
EntityManager.GetAllUniqueSharedComponentData(this.grids);
foreach (var grid in this.grids)
{
    var gridQuery = EntityManager.CreateEntityQuery(typeof(GridComponent));
    gridQuery.SetSharedComponentFilter(grid);
    var gridEntities = gridQuery.ToEntityArray(Allocator.TempJob);
    Debug.Log($"Grid: Entities={gridEntities.Length} Size={grid.Size}");
    gridEntities.Dispose();
}

The output is “Grid: Entities=0 Size=0” which implies that there are no entities that reference this Grid SC.
From my understanding that should be impossible as SCs are reference counted and should be destroyed when unused.

I was wondering if just having a private field of the SharedComponent type is enough for the “empty” SC to be created but testing with a simple MonoBehaviour didnt have the same result:

public class SharedComponentTest : MonoBehaviour
{
    private GridComponent TestGrid;
}

This results in 2 questions:

  • Why is this “empty” SharedComponent created in the first place?
  • Why does it (still) exist when it is not referenced anywhere?

Additional context:

  • I am instantiating entities in edit and play mode so it might be created during edit mode and just “stay”

Doc is clear about this GetAllUniqueSharedComponentData()[0] is always there with default value which is zero for all bytes. I don’t know why. but ECS document is clear on this.

Thx. Where did you find this?
I could not find anything about this behaviour here: Class EntityManager | Package Manager UI website
However I just found this other thread about it which states that its really unintuitive:
Question About GetAllUniqueSharedComponentData

Sorry, I can not find it nether. I tied. Could from other place I read about it.

But if you check EntityManager source

        public void GetAllUniqueSharedComponents<T>(List<T> sharedComponentValues)
            where T : struct, ISharedComponentData
        {
            sharedComponentValues.Add(default(T));
            for (var i = 1; i != m_SharedComponentData.Count; i++)
            {
                var data = m_SharedComponentData[i];
                if (data != null && data.GetType() == typeof(T))
                    sharedComponentValues.Add((T)m_SharedComponentData[i]);
            }
        }

Here, sharedComponentValues.Add(default(T)); is why.