diff --git a/Assets/Mirror/Runtime/NetworkClient.cs b/Assets/Mirror/Runtime/NetworkClient.cs
index c859f986b..3719b1640 100644
--- a/Assets/Mirror/Runtime/NetworkClient.cs
+++ b/Assets/Mirror/Runtime/NetworkClient.cs
@@ -204,7 +204,6 @@ public static void Disconnect()
if (connection != null)
{
connection.Disconnect();
- connection.Dispose();
connection = null;
RemoveTransportHandlers();
}
diff --git a/Assets/Mirror/Runtime/NetworkConnection.cs b/Assets/Mirror/Runtime/NetworkConnection.cs
index 1365c95d1..e8db06be4 100644
--- a/Assets/Mirror/Runtime/NetworkConnection.cs
+++ b/Assets/Mirror/Runtime/NetworkConnection.cs
@@ -16,7 +16,7 @@ namespace Mirror
/// NetworkConnection objects can "own" networked game objects. Owned objects will be destroyed on the server by default when the connection is destroyed. A connection owns the player objects created by its client, and other objects with client-authority assigned to the corresponding client.
/// There are many virtual functions on NetworkConnection that allow its behaviour to be customized. NetworkClient and NetworkServer can both be made to instantiate custom classes derived from NetworkConnection by setting their networkConnectionClass member variable.
///
- public abstract class NetworkConnection : IDisposable
+ public abstract class NetworkConnection
{
public readonly HashSet visList = new HashSet();
@@ -83,13 +83,6 @@ internal set
///
public NetworkIdentity identity { get; internal set; }
- ///
- /// A list of the NetworkIdentity objects owned by this connection. This list is read-only.
- /// This includes the player object for the connection - if it has localPlayerAutority set, and any objects spawned with local authority or set with AssignLocalAuthority.
- /// This list can be used to validate messages from clients, to ensure that clients are only trying to control objects that they own.
- ///
- public readonly HashSet clientOwnedObjects = new HashSet();
-
///
/// Setting this to true will log the contents of network message to the console.
///
@@ -130,35 +123,6 @@ internal NetworkConnection(int networkConnectionId)
#pragma warning restore 618
}
- ~NetworkConnection()
- {
- Dispose(false);
- }
-
- ///
- /// Disposes of this connection, releasing channel buffers that it holds.
- ///
- public void Dispose()
- {
- Dispose(true);
- // Take yourself off the Finalization queue
- // to prevent finalization code for this object
- // from executing a second time.
- GC.SuppressFinalize(this);
- }
-
- protected virtual void Dispose(bool disposing)
- {
- foreach (uint netId in clientOwnedObjects)
- {
- if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity))
- {
- identity.clientAuthorityOwner = null;
- }
- }
- clientOwnedObjects.Clear();
- }
-
///
/// Disconnects this connection.
///
@@ -371,15 +335,5 @@ internal void TransportReceive(ArraySegment buffer, int channelId)
Disconnect();
}
}
-
- internal void AddOwnedObject(NetworkIdentity obj)
- {
- clientOwnedObjects.Add(obj.netId);
- }
-
- internal void RemoveOwnedObject(NetworkIdentity obj)
- {
- clientOwnedObjects.Remove(obj.netId);
- }
}
}
diff --git a/Assets/Mirror/Runtime/NetworkConnectionToClient.cs b/Assets/Mirror/Runtime/NetworkConnectionToClient.cs
index c3061dc1b..af3778f94 100644
--- a/Assets/Mirror/Runtime/NetworkConnectionToClient.cs
+++ b/Assets/Mirror/Runtime/NetworkConnectionToClient.cs
@@ -7,6 +7,13 @@ namespace Mirror
{
public class NetworkConnectionToClient : NetworkConnection
{
+ ///
+ /// A list of the NetworkIdentity objects owned by this connection. This list is read-only.
+ /// This includes the player object for the connection - if it has localPlayerAutority set, and any objects spawned with local authority or set with AssignLocalAuthority.
+ /// This list can be used to validate messages from clients, to ensure that clients are only trying to control objects that they own.
+ ///
+ public readonly HashSet clientOwnedObjects = new HashSet();
+
public NetworkConnectionToClient(int networkConnectionId) : base(networkConnectionId)
{
}
@@ -56,6 +63,31 @@ public override void Disconnect()
isReady = false;
Transport.activeTransport.ServerDisconnect(connectionId);
RemoveObservers();
+ DestroyOwnedObjects();
+ }
+
+ internal void AddOwnedObject(NetworkIdentity obj)
+ {
+ clientOwnedObjects.Add(obj);
+ }
+
+ internal void RemoveOwnedObject(NetworkIdentity obj)
+ {
+ clientOwnedObjects.Remove(obj);
+ }
+
+ protected void DestroyOwnedObjects()
+ {
+ // work on copy because the list may be modified when we destroy the objects
+ HashSet objects = new HashSet(clientOwnedObjects);
+ foreach (NetworkIdentity netId in objects)
+ {
+ if (netId != null)
+ {
+ NetworkServer.Destroy(netId.gameObject);
+ }
+ }
+ clientOwnedObjects.Clear();
}
}
-}
+}
\ No newline at end of file
diff --git a/Assets/Mirror/Runtime/NetworkServer.cs b/Assets/Mirror/Runtime/NetworkServer.cs
index a161a983c..333808948 100644
--- a/Assets/Mirror/Runtime/NetworkServer.cs
+++ b/Assets/Mirror/Runtime/NetworkServer.cs
@@ -190,7 +190,6 @@ internal static void RemoveLocalConnection()
if (localConnection != null)
{
localConnection.Disconnect();
- localConnection.Dispose();
localConnection = null;
}
localClientActive = false;
@@ -445,7 +444,6 @@ public static void DisconnectAllConnections()
// call OnDisconnected unless local player in host mode
if (conn.connectionId != 0)
OnDisconnected(conn);
- conn.Dispose();
}
connections.Clear();
}
@@ -1178,16 +1176,6 @@ internal static void SendSpawnMessage(NetworkIdentity identity, NetworkConnectio
/// The connections object to clean up for.
public static void DestroyPlayerForConnection(NetworkConnection conn)
{
- // => destroy what we can destroy.
- HashSet tmp = new HashSet(conn.clientOwnedObjects);
- foreach (uint netId in tmp)
- {
- if (NetworkIdentity.spawned.TryGetValue(netId, out NetworkIdentity identity))
- {
- Destroy(identity.gameObject);
- }
- }
-
if (conn.identity != null)
{
DestroyObject(conn.identity, true);