Search Unity

A Native Collection (...) resulting in a memory leak (Hybrid Renderer V2), prefabs!

Discussion in 'Entity Component System' started by hangarter, Oct 18, 2021.

  1. hangarter

    hangarter

    Joined:
    Mar 14, 2016
    Posts:
    23
    Hi All,

    So I have what I think it's a very simple setup trying to instantiate prefabs, where I have one authoring object holding a prefab, then I instantiate that prefab X number of times once I click play.
    I don't use any NativeArrays but I keep getting "A Native Collection has not been disposed, resulting in a memory leak".
    No idea what would be the cause of the problem.

    Below is my setup:

    This is my MonoBehaviour with a converter, which maps it to a IComponentData (it has a prefab in it):
    Code (CSharp):
    1. public class Knight : MonoBehaviour, IConvertGameObjectToEntity
    2. {
    3.     public GameObject Prefab;
    4.  
    5.     public void Convert(Entity entity, EntityManager dstManager, GameObjectConversionSystem conversionSystem)
    6.     {
    7.         var prefab = conversionSystem.GetPrimaryEntity(Prefab);
    8.         var component = new KnightPrefabComponent { prefabEntity = prefab };
    9.         dstManager.AddComponentData(entity, component);
    10.     }
    11. }
    Then here is the component definition:
    Code (CSharp):
    1. public struct KnightPrefabComponent : IComponentData
    2. {
    3.     public Entity prefabEntity;
    4. }
    And here I tell ECS that I want prefabs:
    Code (CSharp):
    1. [UpdateInGroup(typeof(GameObjectDeclareReferencedObjectsGroup))]
    2. class PrefabConverterDeclare : GameObjectConversionSystem
    3. {
    4.     protected override void OnUpdate()
    5.     {
    6.         Entities.ForEach((Knight prefabReference) =>
    7.         {
    8.             DeclareReferencedPrefab(prefabReference.Prefab);
    9.         });
    10.     }
    11. }
    This is the code that spawns the entities:
    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Mathematics;
    3. using Unity.Transforms;
    4. using UnityEngine;
    5. using Random = Unity.Mathematics.Random;
    6.  
    7. public class EntitySpawnerSystem : ComponentSystem
    8. {
    9.     private float spawnTimer;
    10.     private Random random;
    11.     private int maxInstances = 10;
    12.     private int instances;
    13.  
    14.     protected override void OnCreate()
    15.     {
    16.         random = new Random(56);
    17.         instances = 0;
    18.     }
    19.     protected override void OnUpdate()
    20.     {
    21.         //spawnTimer -= Time.DeltaTime;
    22.  
    23.         if (instances < maxInstances)
    24.         {
    25.             instances++;
    26.             //spawnTimer = .01f;
    27.             Debug.Log($"Instance: {instances}");
    28.             Entities.ForEach((ref KnightPrefabComponent component) =>
    29.             {
    30.                 var entity = EntityManager.Instantiate(component.prefabEntity);
    31.                 EntityManager.SetComponentData(entity, new Translation()
    32.                 {
    33.                     Value = new float3(random.NextFloat(0, 200), 0, random.NextFloat(0, 200))
    34.                 });
    35.             });
    36.         }
    37.     }
    38.  
    39. }
    40.  
    Stacktrace (none of my classes shows up!):

    A Native Collection has not been disposed, resulting in a memory leak. Allocated from:
    Unity.Collections.NativeArray`1:.ctor(Int32, Allocator, NativeArrayOptions)
    Unity.Scenes.SceneSectionStreamingSystem:ExtractEntityRemapRefs(EntityManager, NativeArray`1&) (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Scenes\SceneSectionStreamingSystem.cs:257)
    Unity.Scenes.SceneSectionStreamingSystem:MoveEntities(EntityManager, Entity) (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Scenes\SceneSectionStreamingSystem.cs:198)
    Unity.Scenes.SceneSectionStreamingSystem:UpdateLoadOperation(AsyncLoadSceneOperation, World, Entity) (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Scenes\SceneSectionStreamingSystem.cs:371)
    Unity.Scenes.SceneSectionStreamingSystem:ProcessActiveStreams() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Scenes\SceneSectionStreamingSystem.cs:314)
    Unity.Scenes.SceneSectionStreamingSystem:OnUpdate() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Scenes\SceneSectionStreamingSystem.cs:531)
    Unity.Entities.ComponentSystem:Update() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystem.cs:114)
    Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystemGroup.cs:472)
    Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystemGroup.cs:417)
    Unity.Entities.ComponentSystem:Update() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystem.cs:114)
    Unity.Entities.ComponentSystemGroup:UpdateAllSystems() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystemGroup.cs:472)
    Unity.Entities.ComponentSystemGroup:OnUpdate() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystemGroup.cs:417)
    Unity.Entities.ComponentSystem:Update() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ComponentSystem.cs:114)
    Unity.Entities.DummyDelegateWrapper:TriggerUpdate() (at Library\PackageCache\com.unity.entities@0.17.0-preview.42\Unity.Entities\ScriptBehaviourUpdateOrder.cs:333)
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    I just looked at the code in that stack trace, and that can only happen if an exception is thrown somewhere in the scene streaming logic. In particular, it seems EntityManager.MoveEntitiesFrom would be the source of such an exception.
     
  3. hangarter

    hangarter

    Joined:
    Mar 14, 2016
    Posts:
    23
    After finding the cause I thought I could share in case someone needs it.
    For a bit more context, my setup has a subscene, and that was happening because the Subscene GameObject had the "ConvertToEntity" checkbox in the Inspector checked.
    Below an screenshot of the setup (how it should be):
    upload_2021-10-19_15-34-55.png
     
    andywatts and apkdev like this.