2007-03-08 Mike Kestner <mkestner@novell.com>

* glib/*.cs : add try/catch blocks to native callback
	methods for DestroyNotify, Copy/Free, and ToggleNotify handlers.  
	Raise UnhandledException events on catches.  

svn path=/trunk/gtk-sharp/; revision=73965
This commit is contained in:
Mike Kestner 2007-03-08 20:28:24 +00:00
parent da407f8ed7
commit 57810954a3
10 changed files with 91 additions and 42 deletions

View file

@ -1,3 +1,9 @@
2007-03-08 Mike Kestner <mkestner@novell.com>
* glib/*.cs : add try/catch blocks to native callback
methods for DestroyNotify, Copy/Free, and ToggleNotify handlers.
Raise UnhandledException events on catches.
2007-03-06 Mike Kestner <mkestner@novell.com> 2007-03-06 Mike Kestner <mkestner@novell.com>
* generator/Signal.cs : add try/catch blocks to native callback * generator/Signal.cs : add try/catch blocks to native callback

View file

@ -96,11 +96,15 @@ namespace GLib {
// object that owned the native delegate wrapper no longer // object that owned the native delegate wrapper no longer
// exists and the instance of the delegate itself is removed from the hash table. // exists and the instance of the delegate itself is removed from the hash table.
private void OnDestroy (IntPtr data) { private void OnDestroy (IntPtr data) {
try {
lock (typeof (DelegateWrapper)) { lock (typeof (DelegateWrapper)) {
if (instances.ContainsKey (this)) { if (instances.ContainsKey (this)) {
instances.Remove (this); instances.Remove (this);
} }
} }
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
} }
} }
} }

View file

@ -55,6 +55,7 @@ namespace GLib {
Console.Error.WriteLine ("Exception in Gtk# callback delegate"); Console.Error.WriteLine ("Exception in Gtk# callback delegate");
Console.Error.WriteLine (" Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception."); Console.Error.WriteLine (" Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception.");
Console.Error.WriteLine (e); Console.Error.WriteLine (e);
Console.Error.WriteLine (new System.Diagnostics.StackTrace (true));
Environment.Exit (1); Environment.Exit (1);
} }

View file

@ -27,6 +27,7 @@ namespace GLib {
using System.Collections; using System.Collections;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
[CDeclCallback]
public delegate bool IdleHandler (); public delegate bool IdleHandler ();
public class Idle { public class Idle {
@ -40,12 +41,17 @@ namespace GLib {
public bool Handler () public bool Handler ()
{ {
try {
IdleHandler idle_handler = (IdleHandler) real_handler; IdleHandler idle_handler = (IdleHandler) real_handler;
bool cont = idle_handler (); bool cont = idle_handler ();
if (!cont) if (!cont)
Remove (); Remove ();
return cont; return cont;
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
return false;
} }
} }

View file

@ -56,18 +56,28 @@ namespace GLib {
static IntPtr Copy (IntPtr ptr) static IntPtr Copy (IntPtr ptr)
{ {
try {
if (ptr == IntPtr.Zero) if (ptr == IntPtr.Zero)
return ptr; return ptr;
GCHandle gch = (GCHandle) ptr; GCHandle gch = (GCHandle) ptr;
return (IntPtr) GCHandle.Alloc (gch.Target); return (IntPtr) GCHandle.Alloc (gch.Target);
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
return IntPtr.Zero;
} }
static void Free (IntPtr ptr) static void Free (IntPtr ptr)
{ {
try {
if (ptr == IntPtr.Zero) if (ptr == IntPtr.Zero)
return; return;
GCHandle gch = (GCHandle) ptr; GCHandle gch = (GCHandle) ptr;
gch.Free (); gch.Free ();
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
} }
public static IntPtr WrapObject (object obj) public static IntPtr WrapObject (object obj)

View file

@ -347,6 +347,7 @@ namespace GLib {
void NotifyCallback (IntPtr handle, IntPtr pspec, IntPtr gch) void NotifyCallback (IntPtr handle, IntPtr pspec, IntPtr gch)
{ {
try {
GLib.Signal sig = ((GCHandle) gch).Target as GLib.Signal; GLib.Signal sig = ((GCHandle) gch).Target as GLib.Signal;
if (sig == null) if (sig == null)
throw new Exception("Unknown signal GC handle received " + gch); throw new Exception("Unknown signal GC handle received " + gch);
@ -356,6 +357,9 @@ namespace GLib {
args.Args[0] = pspec; args.Args[0] = pspec;
NotifyHandler handler = (NotifyHandler) sig.Handler; NotifyHandler handler = (NotifyHandler) sig.Handler;
handler (GLib.Object.GetObject (handle), args); handler (GLib.Object.GetObject (handle), args);
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
} }
void ConnectNotification (string signal, NotifyHandler handler) void ConnectNotification (string signal, NotifyHandler handler)

View file

@ -56,11 +56,15 @@ namespace GLib {
delegate void SignalDestroyNotify (IntPtr data, IntPtr obj); delegate void SignalDestroyNotify (IntPtr data, IntPtr obj);
static void OnNativeDestroy (IntPtr data, IntPtr obj) static void OnNativeDestroy (IntPtr data, IntPtr obj)
{ {
try {
GCHandle gch = (GCHandle) data; GCHandle gch = (GCHandle) data;
Signal s = gch.Target as Signal; Signal s = gch.Target as Signal;
s.DisconnectHandler (s.before_id); s.DisconnectHandler (s.before_id);
s.DisconnectHandler (s.after_id); s.DisconnectHandler (s.after_id);
gch.Free (); gch.Free ();
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
} }
private Signal (GLib.Object obj, string signal_name, Delegate marshaler) private Signal (GLib.Object obj, string signal_name, Delegate marshaler)

View file

@ -24,6 +24,7 @@ namespace GLib {
using System; using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
[CDeclCallback]
public delegate bool TimeoutHandler (); public delegate bool TimeoutHandler ();
public class Timeout { public class Timeout {
@ -37,12 +38,17 @@ namespace GLib {
public bool Handler () public bool Handler ()
{ {
try {
TimeoutHandler timeout_handler = (TimeoutHandler) real_handler; TimeoutHandler timeout_handler = (TimeoutHandler) real_handler;
bool cont = timeout_handler (); bool cont = timeout_handler ();
if (!cont) if (!cont)
Remove (); Remove ();
return cont; return cont;
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
return false;
} }
} }

View file

@ -74,7 +74,7 @@ namespace GLib {
if (weak.IsAlive) if (weak.IsAlive)
reference = weak.Target; reference = weak.Target;
else else
throw new Exception ("Toggling dead object wrapper"); throw new Exception ("Toggling dead wrapper");
} }
} }
@ -83,9 +83,13 @@ namespace GLib {
static void RefToggled (IntPtr data, IntPtr handle, bool is_last_ref) static void RefToggled (IntPtr data, IntPtr handle, bool is_last_ref)
{ {
try {
GCHandle gch = (GCHandle) data; GCHandle gch = (GCHandle) data;
ToggleRef tref = gch.Target as ToggleRef; ToggleRef tref = gch.Target as ToggleRef;
tref.Toggle (is_last_ref); tref.Toggle (is_last_ref);
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
} }
static ToggleNotifyHandler toggle_notify_callback; static ToggleNotifyHandler toggle_notify_callback;

View file

@ -34,10 +34,14 @@ namespace GLib {
delegate void DestroyNotify (IntPtr data); delegate void DestroyNotify (IntPtr data);
static void OnNativeDestroy (IntPtr data) static void OnNativeDestroy (IntPtr data)
{ {
try {
GCHandle gch = (GCHandle) data; GCHandle gch = (GCHandle) data;
WeakObject obj = gch.Target as WeakObject; WeakObject obj = gch.Target as WeakObject;
obj.Dispose (); obj.Dispose ();
gch.Free (); gch.Free ();
} catch (Exception e) {
ExceptionManager.RaiseUnhandledException (e, false);
}
} }
void Dispose () void Dispose ()