[Gtk-sharp-list] [PATCH] ThreadNotify

Gonzalo Paniagua Javier gonzalo@ximian.com
Fri, 17 Oct 2003 19:28:06 +0200


--=-3lofS58xy3AJrvuYO85J
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

We are leaking the 2 fds of the pipe and the GSource created in
gdk_input_add (ThreadNotify.cs).

The attached patch adds Close (), IDisposable pattern and a finalizer to
ThreadNotify.

Ok to commit?

-Gonzalo


--=-3lofS58xy3AJrvuYO85J
Content-Disposition: attachment; filename=threadnotify.patch
Content-Type: text/x-patch; name=threadnotify.patch; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Index: gtk/ThreadNotify.cs
===================================================================
RCS file: /cvs/public/gtk-sharp/gtk/ThreadNotify.cs,v
retrieving revision 1.6
diff -u -r1.6 ThreadNotify.cs
--- gtk/ThreadNotify.cs	30 Aug 2003 00:26:38 -0000	1.6
+++ gtk/ThreadNotify.cs	17 Oct 2003 17:31:14 -0000
@@ -23,13 +23,13 @@
 	/// </summary>
 	/// <remarks/>
 	///   
-	public class ThreadNotify {
+	public class ThreadNotify : IDisposable {
 
 		//
 		// DllImport functions from Gtk
 		//
 		[DllImport ("libgtk-win32-2.0-0.dll")]
-		private static extern int gdk_input_add (int s, int cond, GdkInputFunction f, IntPtr data);
+		private static extern uint gdk_input_add (int s, int cond, GdkInputFunction f, IntPtr data);
 		public delegate void GdkInputFunction (IntPtr data, int source, int cond);
 
 		//
@@ -44,9 +44,13 @@
 		[DllImport ("libc.so.6")]
 		private static extern unsafe int write (int fd, byte *b, int count);
 
+		[DllImport ("libc.so.6")]
+		private static extern int close (int fd);
 		
 		GdkInputFunction notify_pipe;
 		int [] pipes;
+		bool disposed;
+		uint tag;
 
 		ReadyEvent re;
 
@@ -59,7 +63,7 @@
 			notify_pipe = new GdkInputFunction (NotifyPipe);
 			pipes = new int [2];
 			pipe (pipes);
-			gdk_input_add (pipes [0], 1, notify_pipe, (IntPtr) 0);
+			tag = gdk_input_add (pipes [0], 1, notify_pipe, (IntPtr) 0);
 			this.re = re;
 		}
 		
@@ -95,6 +99,34 @@
 					notified = true;
 				}
 			}
+		}
+
+		public void Close ()
+		{
+			Dispose (true);
+		}
+
+		~ThreadNotify ()
+		{
+			Dispose (false);
+		}
+
+		void IDisposable.Dispose ()
+		{
+			Dispose (true);
+		}
+		
+		protected virtual void Dispose (bool disposing)
+		{
+			if (!disposed) {
+				disposed = true;
+				GLib.Source.Remove (tag);
+				close (pipes [1]);
+				close (pipes [0]);
+			}
+
+			pipes = null;
+			notify_pipe = null;
 		}
 	}
 }

--=-3lofS58xy3AJrvuYO85J--