Bladeren bron

Entity Dispose需要清理集合,防止对象池泄漏

tanghai 8 jaren geleden
bovenliggende
commit
2388214906

+ 1 - 1
Client-Server.sln

@@ -1,7 +1,7 @@
 
 Microsoft Visual Studio Solution File, Format Version 12.00
 # Visual Studio 15
-VisualStudioVersion = 15.0.27130.2003
+VisualStudioVersion = 15.0.27130.2010
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Plugins", "Unity\Unity.Plugins.csproj", "{D1FDB199-0FB7-099D-3771-C6A942E4E326}"
 EndProject

+ 1 - 0
Server/App/Properties/launchSettings.json

@@ -2,6 +2,7 @@
   "profiles": {
     "Server.App": {
       "commandName": "Project",
+      "commandLineArgs": "--appId=1 --appType=AllServer --config=../Config/StartConfig/LocalAllServer.txt",
       "workingDirectory": "../../Bin"
     }
   }

+ 11 - 36
Unity/Assets/Scripts/Base/Object/Entity.cs

@@ -13,7 +13,7 @@ namespace Model
 
 		[BsonElement]
 		[BsonIgnoreIfNull]
-		private HashSet<Component> components;
+		private HashSet<Component> components = new HashSet<Component>();
 
 		[BsonIgnore]
 		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
@@ -48,6 +48,9 @@ namespace Model
 					Log.Error(e.ToString());
 				}
 			}
+
+			this.components.Clear();
+			this.componentDict.Clear();
 		}
 
 		public K AddComponent<K>() where K : Component, new()
@@ -59,11 +62,6 @@ namespace Model
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
@@ -80,12 +78,7 @@ namespace Model
 			{
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
-
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
+			
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
@@ -102,12 +95,7 @@ namespace Model
 			{
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
-
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
+			
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
@@ -125,11 +113,6 @@ namespace Model
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
@@ -146,12 +129,9 @@ namespace Model
 				return;
 			}
 
-			this.components?.Remove(component);
+			this.components.Remove(component);
 			this.componentDict.Remove(typeof(K));
-			if (this.components != null && this.components.Count == 0)
-			{
-				this.components = null;
-			}
+
 			component.Dispose();
 		}
 
@@ -165,10 +145,7 @@ namespace Model
 
 			this.components?.Remove(component);
 			this.componentDict.Remove(type);
-			if (this.components != null && this.components.Count == 0)
-			{
-				this.components = null;
-			}
+
 			component.Dispose();
 		}
 
@@ -198,11 +175,9 @@ namespace Model
 			try
 			{
 				ObjectEvents.Instance.Add(this);
-				if (this.components != null && this.components.Count == 0)
-				{
-					this.components = null;
-				}
+
 				this.componentDict.Clear();
+
 				if (this.components != null)
 				{
 					foreach (Component component in this.components)

+ 14 - 31
Unity/Hotfix/Base/Object/Entity.cs

@@ -2,15 +2,21 @@
 using System.Collections.Generic;
 using System.Linq;
 using Model;
+using MongoDB.Bson.Serialization.Attributes;
 
 namespace Hotfix
 {
+	[BsonIgnoreExtraElements]
 	public class Entity : Disposer
 	{
+		[BsonIgnore]
 		public Entity Parent { get; set; }
-		
+
+		[BsonElement]
+		[BsonIgnoreIfNull]
 		private HashSet<Component> components = new HashSet<Component>();
-		
+
+		[BsonIgnore]
 		private Dictionary<Type, Component> componentDict = new Dictionary<Type, Component>();
 
 		protected Entity()
@@ -43,6 +49,9 @@ namespace Hotfix
 					Log.Error(e.ToString());
 				}
 			}
+
+			this.components.Clear();
+			this.componentDict.Clear();
 		}
 
 		public K AddComponent<K>() where K : Component, new()
@@ -54,11 +63,6 @@ namespace Hotfix
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
@@ -76,11 +80,6 @@ namespace Hotfix
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
@@ -98,11 +97,6 @@ namespace Hotfix
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
@@ -120,11 +114,6 @@ namespace Hotfix
 				throw new Exception($"AddComponent, component already exist, id: {this.Id}, component: {typeof(K).Name}");
 			}
 
-			if (this.components == null)
-			{
-				this.components = new HashSet<Component>();
-			}
-
 			if (component is ISerializeToEntity)
 			{
 				this.components.Add(component);
@@ -141,12 +130,9 @@ namespace Hotfix
 				return;
 			}
 
-			this.components?.Remove(component);
+			this.components.Remove(component);
 			this.componentDict.Remove(typeof(K));
-			if (this.components != null && this.components.Count == 0)
-			{
-				this.components = null;
-			}
+
 			component.Dispose();
 		}
 
@@ -160,10 +146,7 @@ namespace Hotfix
 
 			this.components?.Remove(component);
 			this.componentDict.Remove(type);
-			if (this.components != null && this.components.Count == 0)
-			{
-				this.components = null;
-			}
+
 			component.Dispose();
 		}