Parcourir la source

如果Buff忘记主动Dispose释放Timer回调,垃圾回收时也需要释放Timer回调.

tanghai il y a 11 ans
Parent
commit
6d0b66e47e
1 fichiers modifiés avec 22 ajouts et 11 suppressions
  1. 22 11
      CSharp/Game/Model/Buff.cs

+ 22 - 11
CSharp/Game/Model/Buff.cs

@@ -67,6 +67,28 @@ namespace Model
 			}
         }
 
+		protected void Dispose(bool disposing)
+		{
+			if (this.Expiration == 0)
+			{
+				return;
+			}
+
+			// Buff在垃圾回收或者主动Dispose,都需要释放Timer回调
+			World.Instance.GetComponent<TimerComponent>().Remove(this.TimerId);
+			this.expiration = 0;
+		}
+
+		~Buff()
+		{
+			this.Dispose(false);
+		}
+
+		public void Dispose()
+		{
+			this.Dispose(true);
+		}
+
 		public override void BeginInit()
 		{
 			base.BeginInit();
@@ -109,16 +131,5 @@ namespace Model
 				this.ownerId = value;
 			}
 		}
-
-		public void Dispose()
-		{
-			if (this.Expiration == 0)
-			{
-				return;
-			}
-			
-			World.Instance.GetComponent<TimerComponent>().Remove(this.TimerId);
-			this.expiration = 0;
-		}
 	}
 }