From c490d9faa04e16b81f10885ecfce03094d13b41f Mon Sep 17 00:00:00 2001 From: Robin Rolf Date: Tue, 8 Jan 2019 22:12:17 +0100 Subject: [PATCH] Correct order of Destroy in RemovePlayer (#262) The RemovePlayerController call sets the playerController to null, causing a null dereference (and a memory leak as pointed out by @paulpach, since gameobjects are never destroyed) --- Mirror/Runtime/ClientScene.cs | 3 ++- Mirror/Runtime/NetworkServer.cs | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Mirror/Runtime/ClientScene.cs b/Mirror/Runtime/ClientScene.cs index af822e954..da3a4ac52 100644 --- a/Mirror/Runtime/ClientScene.cs +++ b/Mirror/Runtime/ClientScene.cs @@ -121,10 +121,11 @@ public static bool RemovePlayer() RemovePlayerMessage msg = new RemovePlayerMessage(); s_ReadyConnection.Send((short)MsgType.RemovePlayer, msg); + Object.Destroy(s_ReadyConnection.playerController.gameObject); + s_ReadyConnection.RemovePlayerController(); s_LocalPlayer = null; - Object.Destroy(s_ReadyConnection.playerController.gameObject); return true; } return false; diff --git a/Mirror/Runtime/NetworkServer.cs b/Mirror/Runtime/NetworkServer.cs index 405d9f60b..8f4c3f3bf 100644 --- a/Mirror/Runtime/NetworkServer.cs +++ b/Mirror/Runtime/NetworkServer.cs @@ -821,8 +821,8 @@ static void OnRemovePlayerMessage(NetworkMessage netMsg) { if (netMsg.conn.playerController != null) { - netMsg.conn.RemovePlayerController(); Destroy(netMsg.conn.playerController.gameObject); + netMsg.conn.RemovePlayerController(); } else {