gtk: Fix a KeyNotFoundException regression in Destroyed event

The migration to generic collections [1] caused another regression: a
KeyNotFoundException was being thrown (instead of returning null like
HashTable did) when using the Destroyed event of the Gtk.Widget class.

[1] 6850b343ca

A particular example of this problem is Banshee's Import Media feature,
which was generating the following stacktrace:

[1 Debug 23:24:36.898] Starting - Importing Media
Marshaling activate 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.Collections.Generic.KeyNotFoundException: The given key was not
present in the dictionary.
  at System.Collections.Generic.Dictionary`2[System.IntPtr,System.Delegate].get_Item
(IntPtr key) [0x00000] in <filename unknown>:0
  at Gtk.Widget.add_Destroyed (System.EventHandler value) [0x00000] in
/home/knocte/gtk-sharp/gtk/Widget.cs:333
  at Hyena.Widgets.AnimatedWidget..ctor (Gtk.Widget widget, UInt32
duration, Easing easing, Blocking blocking, Boolean horizontal)
[0x0004d] in /home/knocte/banshee/src/Hyena/Hyena.Gui/Hyena.Widgets/AnimatedWidget.cs:78
This commit is contained in:
Andrés G. Aragoneses 2013-09-24 01:56:58 +02:00 committed by Bertrand Lorentz
parent 2388d09bb7
commit 7ce1457c13

View file

@ -330,11 +330,15 @@ namespace Gtk {
[GLib.Signal("destroy")]
public event EventHandler Destroyed {
add {
EventHandler handler = (EventHandler) DestroyHandlers [Handle];
Delegate delegate_handler;
DestroyHandlers.TryGetValue (Handle, out delegate_handler);
var handler = delegate_handler as EventHandler;
DestroyHandlers [Handle] = Delegate.Combine (handler, value);
}
remove {
EventHandler handler = (EventHandler) DestroyHandlers [Handle];
Delegate delegate_handler;
DestroyHandlers.TryGetValue (Handle, out delegate_handler);
var handler = delegate_handler as EventHandler;
handler = (EventHandler) Delegate.Remove (handler, value);
if (handler != null)
DestroyHandlers [Handle] = handler;