[ease/bindings] [bindings] Automatically drop bindings when objects are finalized



commit 7d77332e600f5c49381f1f05b463462616a21063
Author: Nate Stedman <natesm gmail com>
Date:   Fri Aug 20 14:03:54 2010 -0400

    [bindings] Automatically drop bindings when objects are finalized

 ease-bindings/bindings.vala |   24 ++++++++++++++++++++++--
 1 files changed, 22 insertions(+), 2 deletions(-)
---
diff --git a/ease-bindings/bindings.vala b/ease-bindings/bindings.vala
index 1103ec1..6b45626 100644
--- a/ease-bindings/bindings.vala
+++ b/ease-bindings/bindings.vala
@@ -36,6 +36,10 @@ public static class Bindings
 		
 		// keep track of the binding
 		bindings.add(new Binding(object1, property1, object2, property2));
+		
+		// when an object is finalized, destroy all bindings for it
+		object1.weak_ref(on_finalize);
+		object2.weak_ref(on_finalize);
 	}
 	
 	public static void drop(GLib.Object object1, string property1,
@@ -100,10 +104,26 @@ public static class Bindings
 		to.set_property(to_prop, storage);
 	}
 	
+	private static void on_finalize(GLib.Object object)
+	{
+		if (bindings.size < 1) return;
+		
+		var itr = bindings.iterator();
+		for (itr.first();; itr.next())
+		{
+			var binding = itr.get() as Binding;
+			if (binding.obj1 == object || binding.obj2 == object)
+			{
+				itr.remove();
+			}
+			if (!itr.has_next()) break;
+		}
+	}
+	
 	private class Binding : GLib.Object
 	{
-		public GLib.Object obj1;
-		public GLib.Object obj2;
+		public weak GLib.Object obj1;
+		public weak GLib.Object obj2;
 		public string prop1;
 		public string prop2;
 		public bool silence = false;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]