Skip to content

Commit e5575e0

Browse files
authored
fix: eliminate bad use-after-free(destroy) pattern (#1068)
* fix: eliminate bad use-after-free(destroy) pattern * remove obsolete comment
1 parent 0db6789 commit e5575e0

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,20 @@ public static void NetworkHide(List<NetworkObject> networkObjects, ulong clientI
380380
}
381381
}
382382

383+
private bool m_ApplicationQuitting = false;
384+
385+
private void OnApplicationQuit()
386+
{
387+
m_ApplicationQuitting = true;
388+
}
389+
383390
private void OnDestroy()
384391
{
392+
if (m_ApplicationQuitting)
393+
{
394+
return;
395+
}
396+
385397
if (NetworkManager != null && NetworkManager.IsListening && NetworkManager.IsServer == false && IsSpawned)
386398
{
387399
throw new NotServerException($"Destroy a spawned {nameof(NetworkObject)} on a non-host client is not valid. Call {nameof(Destroy)} or {nameof(Despawn)} on the server/host instead.");

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -685,8 +685,12 @@ internal void OnDespawnObject(NetworkObject networkObject, bool destroyGameObjec
685685
}
686686
}
687687

688-
var gobj = networkObject.gameObject;
688+
if (SpawnedObjects.Remove(networkObject.NetworkObjectId))
689+
{
690+
SpawnedObjectsList.Remove(networkObject);
691+
}
689692

693+
var gobj = networkObject.gameObject;
690694
if (destroyGameObject && gobj != null)
691695
{
692696
if (NetworkManager.PrefabHandler.ContainsHandler(networkObject))
@@ -698,14 +702,6 @@ internal void OnDespawnObject(NetworkObject networkObject, bool destroyGameObjec
698702
UnityEngine.Object.Destroy(gobj);
699703
}
700704
}
701-
702-
// for some reason, we can get down here and SpawnedObjects for this
703-
// networkId will no longer be here, even as we check this at the start
704-
// of the function
705-
if (SpawnedObjects.Remove(networkObject.NetworkObjectId))
706-
{
707-
SpawnedObjectsList.Remove(networkObject);
708-
}
709705
}
710706
}
711707
}

0 commit comments

Comments
 (0)