Bug 6315 - debugger fails to catch arbitrary exception through signal event handler
Summary: debugger fails to catch arbitrary exception through signal event handler
Status: CONFIRMED
Alias: None
Product: Xamarin Studio
Classification: Desktop
Component: Debugger (show other bugs)
Version: Trunk
Hardware: All All
: Low enhancement
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-07-27 23:26 UTC by Atsushi Eno
Modified: 2017-08-04 22:30 UTC (History)
1 user (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments

Description Atsushi Eno 2012-07-27 23:26:04 UTC
With the simple code below, I cannot get the debugger catch the NRE at "button_Clicked" method:

using System;
using Gtk;

namespace uitest
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			Application.Init ();
			
			var win = new Window (WindowType.Toplevel){
				DefaultWidth = 400,
				DefaultHeight = 300
			};
			win.DeleteEvent += (sender, e) => {
				Application.Quit ();
				e.RetVal = true;
			};
			
			var button = new Button("test");
			button.Clicked += button_Clicked;
			win.Add (button);
			
			win.ShowAll ();
			Application.Run ();
		}
		
		static void button_Clicked (object sender, EventArgs e)
		{
			string s = null;
			s.ToString (); // null reference exception
		}
	}
}

This *should* raise an error, but the problem is the debugger cannot catch that. I can set a breakpoint to "string s = null;" line, so the debugger can stop it. (This may be rather an sdb issue, I'm not sure.)

Marshaling clicked signal
Exception in Gtk# callback delegate
  Note: Applications can use GLib.ExceptionManager.UnhandledException to handle the exception.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object
  at uitest.MainClass.button_Clicked (System.Object sender, System.EventArgs e) [0x00003] in /home/atsushi/Projects/ConsoleApp3/ConsoleApp3/Main.cs:33 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000d5] in /svn/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:229 
  --- End of inner exception stack trace ---
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x000eb] in /svn/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:237 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /svn/mono/mcs/class/corlib/System.Reflection/MethodBase.cs:101 
  at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000e4] in /svn/mono/mcs/class/corlib/System/Delegate.cs:424 
  at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00018] in /svn/mono/mcs/class/corlib/System/MulticastDelegate.cs:72 
  at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in /svn/mono/mcs/class/corlib/System/Delegate.cs:398 
  at GLib.Signal.ClosureInvokedCB (System.Object o, GLib.ClosureInvokedArgs args) [0x00050] in /svn/gtk-sharp/glib/Signal.cs:195 
  at GLib.SignalClosure.Invoke (GLib.ClosureInvokedArgs args) [0x00011] in /svn/gtk-sharp/glib/SignalClosure.cs:118 
  at GLib.SignalClosure.MarshalCallback (IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data) [0x0007d] in /svn/gtk-sharp/glib/SignalClosure.cs:146 
   at GLib.ExceptionManager.RaiseUnhandledException(System.Exception e, Boolean is_terminal) in /svn/gtk-sharp/glib/ExceptionManager.cs:line 58
   at GLib.SignalClosure.MarshalCallback(IntPtr raw_closure, IntPtr return_val, UInt32 n_param_vals, IntPtr param_values, IntPtr invocation_hint, IntPtr marshal_data) in /svn/gtk-sharp/glib/SignalClosure.cs:line 173
   at Gtk.Application.gtk_main()
   at Gtk.Application.Run() in /svn/gtk-sharp/gtk/Application.cs:line 135
   at uitest.MainClass.Main(System.String[] args) in /home/atsushi/Projects/ConsoleApp3/ConsoleApp3/Main.cs:line 27
Comment 1 Jeffrey Stedfast 2013-04-01 14:52:05 UTC
The problem is that Gtk# has its own unhandled exception handling around user event handlers which it catches (and so the runtime can't know that it was "unhandled").

GLib catches it, and if the user hasn't attached their own unhandled exception handler, it prints the stack trace and exits.

You could use the Catchpoints feature in Xamarin Studio to catch these before they get caught by Gtk#'s code.

Note You need to log in before you can comment on or make changes to this bug.