diff --git a/Ragon.Client/Sources/Entity/RagonEntity.cs b/Ragon.Client/Sources/Entity/RagonEntity.cs index 1d03787..ee76844 100644 --- a/Ragon.Client/Sources/Entity/RagonEntity.cs +++ b/Ragon.Client/Sources/Entity/RagonEntity.cs @@ -19,7 +19,7 @@ using Ragon.Protocol; namespace Ragon.Client { - public sealed class RagonEntity + public sealed class RagonEntity: IDisposable { private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer); @@ -266,5 +266,12 @@ namespace Ragon.Client OwnershipChanged?.Invoke(prevOwner, player); } + + public void Dispose() + { + _events.Clear(); + _listeners.Clear(); + _localListeners.Clear(); + } } } \ No newline at end of file diff --git a/Ragon.Client/Sources/RagonClient.cs b/Ragon.Client/Sources/RagonClient.cs index 1c0ee95..25c8159 100644 --- a/Ragon.Client/Sources/RagonClient.cs +++ b/Ragon.Client/Sources/RagonClient.cs @@ -193,6 +193,7 @@ namespace Ragon.Client internal void AssignRoom(RagonRoom room) { + _room?.Dispose(); _room = room; } diff --git a/Ragon.Client/Sources/RagonEntityCache.cs b/Ragon.Client/Sources/RagonEntityCache.cs index 31f841d..27095c8 100644 --- a/Ragon.Client/Sources/RagonEntityCache.cs +++ b/Ragon.Client/Sources/RagonEntityCache.cs @@ -215,10 +215,12 @@ public sealed class RagonEntityCache { if (_entityMap.TryGetValue(entityId, out var entity)) { + _entityMap.Remove(entityId); _entityList.Remove(entity); entity.Detach(payload); + entity.Dispose(); } } diff --git a/Ragon.Client/Sources/RagonRoom.cs b/Ragon.Client/Sources/RagonRoom.cs index 7a9d2b9..0f9244e 100644 --- a/Ragon.Client/Sources/RagonRoom.cs +++ b/Ragon.Client/Sources/RagonRoom.cs @@ -18,7 +18,7 @@ using Ragon.Protocol; namespace Ragon.Client { - public class RagonRoom + public class RagonRoom: IDisposable { private delegate void OnEventDelegate(RagonPlayer player, RagonBuffer serializer); @@ -130,5 +130,14 @@ namespace Ragon.Client public void DestroyEntity(RagonEntity entityId) => DestroyEntity(entityId, null); public void DestroyEntity(RagonEntity entityId, RagonPayload payload) => _entityCache.Destroy(entityId, payload); + + public void Dispose() + { + Cleanup(); + + _events.Clear(); + _listeners.Clear(); + _localListeners.Clear(); + } } } \ No newline at end of file