Search Unity

Bug 2021.3 LTS hanging on a subset of player base when attempting to destroy a gameObject

Discussion in 'Editor & General Support' started by skowroshima, Jun 13, 2022.

  1. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    I have a small portion of my player base getting random crashes. There are no exceptions, Unity just hangs and they have to end the process.

    I was able to get a player to send me a dump file before ending the process.

    It looks like it is hanging in the IL2CPP code generated for when I destroy a gameObject.

    WinDbg stack in the dump file


    Is there anything I could be doing to cause this? Is this just a Unity Bug? I really would like to resolve it, I've gotten a few bad reviews as a result of this.

    The c# this derived execution comes from:
    Code (CSharp):
    1.         // Evaluate invaders
    2.         List<Invader> invadersToRemove = null;
    3.         foreach (Invader invader in mPopulation)
    4.         {
    5.             invader.OverlordUpdate();
    6.             if (invader.mReadyToBeDestroyed)
    7.             {
    8.                 if (invadersToRemove == null) invadersToRemove = new List<Invader>();
    9.                 invadersToRemove.Add(invader);
    10.             }
    11.         }
    12.         if (invadersToRemove != null)
    13.         {
    14.             foreach (Invader invaderToRemove in invadersToRemove)
    15.             {
    16.                 mPopulation.Remove(invaderToRemove);
    17.                 mOverlord.mSpatialManager.RemoveSpatialEntree(invaderToRemove.transform);
    18.                 Destroy(invaderToRemove.gameObject);
    19.             }
    20.         }
    Note: Invader is just a MonoBehaviour added to the game object. I'm destroying instantiated all over my game this same way.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,848
    I can‘t say whether this is the issue but what I normally do when destroying items in a list is to reverse enumerate:

    Code (CSharp):
    1.             for (int i = invadersToRemove.Length - 1; i >=0; i—)
    2.             {
    3. var invaderToRemove = invadersToRemove[i];
    4.                 mPopulation.Remove(invaderToRemove);
    5.                 mOverlord.mSpatialManager.RemoveSpatialEntree(invaderToRemove.transform);
    6.                 Destroy(invaderToRemove.gameObject);
    7.             }
    But this is mainly for cases where you delete an item from the list while enumerating.

    I would rather suspect the culprit within the invader scripts. Consider all edge cases where destroying the object could have adverse side effects, ie delegate not unregistered and receiving a notification just after the object was destroyed.
     
  3. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    Yeah, a reverse iteration vs making a list to destroy, I'm sure that isn't the source.

    Looking closer at the invader, it has a bunch of components, but I don't see anything suspicious (like deligates that could be triggered). I'm going to try and make a version build that disables all the components, and then waits a second before destroying the gameObject... I'll see if that still causes the hanging to occur.
     
  4. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    That did not resolve this issue, the small population is still hanging. The anecdotal feedback is if feels less frequent with the delay. I'm going to bump the destroy delay to 10 seconds and see if that makes an even larger difference.
     
    Last edited: Jun 16, 2022
  5. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
  6. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    Cool, didn't know that was an option, hopefully this helps:
    upload_2022-6-16_20-48-43.png
     
  7. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    Based on the delay before destroying the gameObject seeming to reduce the chances of that happening, I upped the delay to 5 seconds, and in addition to deactivating the components, I'm deactivating the gameObject itself.

    So far that has seemed to reduced the odds of this issue occurring to the point where it hasn't happened in a days worth of testing.

    While this may be good enough for my purposes, obviously this would be good to resolve for others. Nothing should really cause all of Unity to hang when destroying an gameObject.
     
  8. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,674
    Hmm, based on that callstack, it's unlikely the hang is in Unity itself. Is there any chance that script gets into an infinite loop?
     
  9. skowroshima

    skowroshima

    Joined:
    Oct 14, 2018
    Posts:
    75
    No, there is no way it is in a loop in the outer code, if it was, it wouldn't try and destroy the removed invaders in subsequent iterations (and thus end up with that call stack in the dump).

    Like I said, this is happening to a quite small population (2 players of 1000s have reported it). One only started getting it when he upgraded to a 3070. Pretty odd stuff.