Search Unity

Question Leak Detected : Persistent allocates 41 individual allocations.

Discussion in 'Entity Component System' started by Hazzel, May 29, 2023.

  1. Hazzel

    Hazzel

    Joined:
    Feb 26, 2022
    Posts:
    14
    Hello,

    i followed the turbomakesgames tutorial

    Leak Detected : Persistent allocates 41 individual allocations.
    i get leaks: (only posted the first one)


    Found 1 leak(s) from callstack:
    0x00007ffdef4b57ef (1927aa1165b3ede9eaa66390321407e) SpawnAsteroidSystem::SpawnAsteroidSystem.OnUpdate (at C:/Users/hazzel/Unity Projects/dotsHelpMeJessus/Assets/Scripts/Systems/SpawnAsteroidSystem.cs:50)
    0x00007ffdef4b487f (1927aa1165b3ede9eaa66390321407e) edae3ecf9518a834e7286f44182dc0c3
    0x00007ffdef47a095 (e72d37c51499da0ff0994a0de62acbf) Unity.Entities.WorldUnmanagedImpl.Unity.Entities.UnmanagedUpdate_0000153F$BurstDirectCall.Invoke (at C:/Users/hazzel/Unity Projects/dotsHelpMeJessus/Library/PackageCache/com.unity.burst@1.8.4/.Runtime/unknown/unknown:0)
    0x00007ffdef478d0f (e72d37c51499da0ff0994a0de62acbf) 7bf3b3dc1c88cb657fd69b548232391d
    0x000001e934312588 (Mono JIT Code) (wrapper managed-to-native) Unity.Entities.WorldUnmanagedImpl/Unity.Entities.UnmanagedUpdate_0000153F$BurstDirectCall:wrapper_native_indirect_000001EAB0331DC0 (intptr&,void*)
    0x000001e9343120f3 (Mono JIT Code) Unity.Entities.WorldUnmanagedImpl/Unity.Entities.UnmanagedUpdate_0000153F$BurstDirectCall:Invoke (void*)
    0x000001e93431207b (Mono JIT Code) Unity.Entities.WorldUnmanagedImpl:UnmanagedUpdate (void*) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/WorldUnmanaged.cs:831)
    0x000001e934311ce3 (Mono JIT Code) Unity.Entities.WorldUnmanagedImpl:UpdateSystem (Unity.Entities.SystemHandle) (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/WorldUnmanaged.cs:895)
    0x000001e93430d82b (Mono JIT Code) Unity.Entities.ComponentSystemGroup:UpdateAllSystems () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/ComponentSystemGroup.cs:729)
    0x000001e93430d2c3 (Mono JIT Code) Unity.Entities.ComponentSystemGroup:OnUpdate () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/ComponentSystemGroup.cs:693)
    0x000001e93430d103 (Mono JIT Code) Unity.Entities.InitializationSystemGroup:OnUpdate () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/DefaultWorld.cs:170)
    0x000001e93430c4c3 (Mono JIT Code) Unity.Entities.SystemBase:Update () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/SystemBase.cs:420)
    0x000001e93430c1dd (Mono JIT Code) Unity.Entities.ScriptBehaviourUpdateOrder/DummyDelegateWrapper:TriggerUpdate () (at ./Library/PackageCache/com.unity.entities@1.0.10/Unity.Entities/ScriptBehaviourUpdateOrder.cs:528)
    0x000001ea08581518 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
    0x00007ffe36b8e084 (mono-2.0-bdwgc) mono_jit_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/mini/mini-runtime.c:3445)
    0x00007ffe36aceb84 (mono-2.0-bdwgc) do_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3066)
    0x00007ffe36aced1c (mono-2.0-bdwgc) mono_runtime_invoke (at C:/build/output/Unity-Technologies/mono/mono/metadata/object.c:3113)
    0x00007ff7e9b6ee94 (Unity) scripting_method_invoke
    0x00007ff7e9b4f714 (Unity) ScriptingInvocation::Invoke

    the system and the error is on line 50, as far as i understand where it sets the component

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Burst;
    4. using Unity.Collections;
    5. using Unity.Entities;
    6. using Unity.Mathematics;
    7. using Unity.Transforms;
    8. using UnityEngine;
    9.  
    10. [BurstCompile]
    11. [UpdateInGroup(typeof(InitializationSystemGroup))]
    12. public partial struct SpawnAsteroidSystem : ISystem
    13. {
    14.     [BurstCompile]
    15.     public void OnCreate(ref SystemState state)
    16.     {
    17.         state.RequireForUpdate<SolarSystemProperties>();
    18.     }
    19.     [BurstCompile]
    20.     public void OnDestroy(ref SystemState state)
    21.     {
    22.    
    23.     }
    24.     [BurstCompile]
    25.     public void OnUpdate(ref SystemState state)
    26.     {
    27.         state.Enabled = false;
    28.         var solarSystemEntity = SystemAPI.GetSingletonEntity<SolarSystemProperties>();
    29.         var solarSystemAspect = SystemAPI.GetAspect<SolarSystemAspect>(solarSystemEntity);
    30.         var tombstoneOffset = new float3(0f, -2f, 1f);
    31.  
    32.         var ecb = new EntityCommandBuffer(Allocator.Temp);
    33.  
    34.         var builder = new BlobBuilder(Allocator.Temp);
    35.         ref var spawnPoints = ref builder.ConstructRoot<AsteroidSpawnPointsBlob>();
    36.         var arrayBuilder = builder.Allocate(ref spawnPoints.Value, solarSystemAspect.NumberOfAsteroidsToSpawn);
    37.  
    38.         for (int i = 0; i < solarSystemAspect.NumberOfAsteroidsToSpawn; i++)
    39.         {
    40.             var newAsteroidEntity = ecb.Instantiate(solarSystemAspect.AsteroidToSpawn);
    41.             var newAsteroidTransform = solarSystemAspect.GetRandomAsteroidTransform();
    42.             ecb.SetComponent(newAsteroidEntity, newAsteroidTransform);
    43.  
    44.             var newAsteroidSpawnPoint = newAsteroidTransform.Position;// + tombstoneOffset;
    45.             arrayBuilder[i] = newAsteroidSpawnPoint;
    46.  
    47.         }
    48.  
    49.         var blobAsset = builder.CreateBlobAssetReference<AsteroidSpawnPointsBlob>(Allocator.Persistent);
    50.         ecb.SetComponent(solarSystemEntity, new AsteroidSpawnPoints { Value = blobAsset });
    51.         builder.Dispose();
    52.         ecb.Playback(state.EntityManager);
    53.     }
    54. }
    55.  
    56.  
    The IData component:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Entities;
    4. using Unity.Mathematics;
    5. using UnityEngine;
    6.  
    7. public struct AsteroidSpawnPoints : IComponentData
    8. {
    9.     public BlobAssetReference<AsteroidSpawnPointsBlob> Value;
    10.  
    11. }
    12.  
    13. public struct AsteroidSpawnPointsBlob
    14. {
    15.     public BlobArray<float3> Value;
    16. }
    17.  

    the video


    his full github repo with alle the code if needed:
    https://gist.github.com/JohnnyTurbo/b6ce91c1deffb9ac563e7b9744dff44e#file-brainaspect-cs
     
  2. Arnold_2013

    Arnold_2013

    Joined:
    Nov 24, 2013
    Posts:
    287
    I don't know much about blobs. But in general I guess a leak would point to you allocating memory without freeing it and not having a way to access this data anymore.

    In the unity menu you might be able to do Jobs -> Burst -> Safety checks -> Off, maybe this will remove the error. Should you just want to run the application.

    To solve the issue you would have to know what is leaking and dispose it properly. But this code does not look very good to me, so I would not want to burn my hands on it.

    I would change "var blobAsset" to a public field (public BlobAssetReference <AsteroidSpawnPointsBlob> blobAsset;) and hope for the best. Or just look for more examples and see how they do it.