Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug help with debugging of this error "The Handle has already been released."

Discussion in 'Entity Component System' started by nicloay, Jul 10, 2023.

  1. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    535
    Hello,
    I have the scene and can start and play it, but sometimes (like on 5 or 10th run, I can't identify the reason when it happens) It stops running.
    I'm not able to start the scene in the editor anymore, and I need to restart the editor itself to continue able to run this scene.

    I enabled all debug tools in the editor (collection checks, enable jobs debugger, etc.)
    If I start play mode with pause, an error appears on the 2d or 3d frame step.

    here is a stack trace
    and nothing special happens in my class that trigger this error (I only run command buffer from fixed update step)
    upload_2023-7-10_17-37-55.png
    and here is system itself
    Code (CSharp):
    1.     public partial class AddDamageBufferForHealthOwnersSystem : SystemBase
    2.     {
    3.         protected override void OnUpdate()
    4.         {
    5.             var ecb = new EntityCommandBuffer(Allocator.Temp);
    6.             foreach (var (_, entity) in SystemAPI.Query<MaxHealth>().WithNone<AddDamage>().WithEntityAccess())
    7.             {
    8.                 ecb.AddBuffer<AddDamage>(entity);
    9.             }
    10.             ecb.Playback(EntityManager);
    11.         }
    12.     }
    any ideas on what else I can do to identify the reason for this problem?
    thanks.
     
  2. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    For this specific case its better to use AddComponent with EntityQuery. No need to iterate each entity one-by-one.

    As to why ECB fails - no idea.
    Try grabbing ECB from the sync point (e.g. EndSimulationECBSystem) instead. See if it helps.
    (Don't playback immediately)
     
  3. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    535
    Thanks for the suggestion.
    I removed that system at all and baked the required component, but it didn't solve the error, and it still crash with AtomicSafetyHandle similar thing but now from a different place. As I said it's not permanent, it appeared after several runs. So would be great to find a way how to debug this error.
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    Try running in prior version of Entities, check if it still happens. Maybe newer one introduced a regression.
    (e.g. 1.0.10)
    Also, try wiping Library just in case.

    One more thing to try - move logic away out of FixedUpdate group. Maybe it is framerate dependent.
     
  5. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    535
    Thanks.
    * I tried a different version of entities (but all was 1.xx) and errors start to appear there as well
    * I also tried to purge the Library folder, the effect was similar to just restarting the editor (the error disappear and then appear after several runs)
    * I'll try to review if I can move anything out of fixed update.
     
  6. gencontain

    gencontain

    Joined:
    Nov 15, 2012
    Posts:
    14
    This sounds like exactly the problem I ran into as well. It seems to be connected to using ECBs. I reported it as a bug but it got closed as cannot reproduce. Eventually I worked around it by never adding components through ECB systems. My only hunch is that it ends up trying to add components to destroyed entities but doesn't report the error correctly for whatever reason.
     
  7. nicloay

    nicloay

    Joined:
    Jul 11, 2012
    Posts:
    535
    I'm not sure if I solved the problem, but I found one of my systems that used an Allocator.TempJob which I didn't dispose of explicitly after usage. I hope that I fixed the issue, I'll update this post if I'll face again that crash.
     
  8. Mockarutan

    Mockarutan

    Joined:
    May 22, 2011
    Posts:
    158
    Any updates on this? I have this problem and I add a lot of components though ECBs, so it's not really possible to remove that for me.
     
  9. gencontain

    gencontain

    Joined:
    Nov 15, 2012
    Posts:
    14
    Ever since I minimized my ECB usage I haven't seen this issue. To clarify I think adding components through ECBs is ok in itself, it's just that if you do end up trying to add components to destroyed entities it may not give you the error it should give you and instead ends up causing this issue. But that's only a hunch, I haven't been able to create a small enough reproduction of this bug so that I'd have something more useful to share.
     
  10. n3b

    n3b

    Joined:
    Nov 16, 2014
    Posts:
    56
    @Mockarutan have you tried using Allocator.TempJob instead of Allocator.Temp on ECBs? (and don't forget to dispose them)
     
  11. Mockarutan

    Mockarutan

    Joined:
    May 22, 2011
    Posts:
    158
    No, I use Temp for most of my ECB's in EFE.Run(). So maybe I should switch from Temp to TempJob then?
     
  12. n3b

    n3b

    Joined:
    Nov 16, 2014
    Posts:
    56