GIO# System.InvalidCastException in VolumeMonitor.MountAdded event

View: New views
4 Messages — Rating Filter:   Alert me  

GIO# System.InvalidCastException in VolumeMonitor.MountAdded event

by Chris Szikszoy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I saw that GIO# bindings landed in the mainline gtk# branch recently and wanted experiment to see if I could replace the old Gnome.VFS in my application.  I've built the latest gtk# from svn and am running into a problem with the VolumeMonitor.Mount* signals.  I keep getting an InvalidCastException when trying to access args.Mount from any of the events (MountAdded, MountRemoved, MountPreUnmount, MountChanged).

I've looked at the generated code and it seems to be failing at here:
public Mount Mount{
        get {
                return (GLib.Mount)GLib.SignalArgs.get_Args()[0];
        }
}

Try running this application to observe:
using System;
using GLib;
using Gtk;
namespace giotest
{
        class MainClass
        {
                public static void Main (string[] args)
                {
                        Gtk.Application.Init ();
                       
                        VolumeMonitor mon = VolumeMonitor.Default;

                        mon.MountAdded += delegate(object o, MountAddedArgs a) {
                                Console.WriteLine ("Mount Added: {0}", a.Mount.Name);
                        };
                       
                        mon.MountRemoved += delegate(object o, MountRemovedArgs a) {
                                Console.WriteLine ("Mount removed: {0}", a.Mount.Name);
                        };
                       
                        Gtk.Application.Run ();
                }
        }
}

The relevant parts of the exception are:
Marshaling mount-added 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.InvalidCastException: Cannot cast from source type to destination type.
  at GLib.MountAddedArgs.get_Mount () [0x00000]
  at giotest.MainClass.<Main>m__0 (System.Object o, GLib.MountAddedArgs a) [0x00000] in /home/chris/Projects/giotest/giotest/Main.cs:1
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (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) [0x00000]
  --- End of inner exception stack trace ---

Should I file a bug?  I found the GTK# bugzilla at bugzilla.novell.com but I wasn't sure if that was the right place.

Thanks in advance for all the help.
- Chris

Re: GIO# System.InvalidCastException in VolumeMonitor.MountAdded event

by Christian Hoff :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Chris Szikszoy wrote:

> I saw that GIO# bindings landed in the mainline gtk# branch recently and
> wanted experiment to see if I could replace the old Gnome.VFS in my
> application.  I've built the latest gtk# from svn and am running into a
> problem with the VolumeMonitor.Mount* signals.  I keep getting an
> InvalidCastException when trying to access args.Mount from any of the events
> (MountAdded, MountRemoved, MountPreUnmount, MountChanged).
>
> I've looked at the generated code and it seems to be failing at here:
> public Mount Mount{
> get {
> return (GLib.Mount)GLib.SignalArgs.get_Args()[0];
> }
> }
>  
I have attached a patch which fixes the issue together with a modified
version of your test program. Mike, could you review?


Christian


Index: generator/Signal.cs
===================================================================
--- generator/Signal.cs (Revision 143910)
+++ generator/Signal.cs (Arbeitskopie)
@@ -271,12 +271,18 @@
  sw.WriteLine ("\t\tpublic " + parms[i].CSType + " " + parms[i].StudlyName + "{");
  if (parms[i].PassAs != "out") {
  sw.WriteLine ("\t\t\tget {");
- sw.WriteLine ("\t\t\t\treturn (" + parms[i].CSType + ") Args[" + i + "];");
+ if (SymbolTable.Table.IsInterface (parms [i].CType))
+ sw.WriteLine ("\t\t\t\treturn {0}Adapter.GetObject (Args [{1}] as GLib.Object);", parms [i].CSType, i);
+ else
+ sw.WriteLine ("\t\t\t\treturn ({0}) Args [{1}];", parms [i].CSType, i);
  sw.WriteLine ("\t\t\t}");
  }
  if (parms[i].PassAs != "") {
  sw.WriteLine ("\t\t\tset {");
- sw.WriteLine ("\t\t\t\tArgs[" + i + "] = (" + parms[i].CSType + ")value;");
+ if (SymbolTable.Table.IsInterface (parms [i].CType))
+ sw.WriteLine ("\t\t\t\tArgs [{0}] = value is {1}Adapter ? (value as {1}Adapter).Implementor : value;", i, parms [i].CSType);
+ else
+ sw.WriteLine ("\t\t\t\tArgs[" + i + "] = (" + parms[i].CSType + ")value;");
  sw.WriteLine ("\t\t\t}");
  }
  sw.WriteLine ("\t\t}");

using System;
using GLib;

namespace giotest
{
        static class MainClass
        {
                static GLib.MainLoop loop;

                public static void Main (string[] args)
                {
                        GLib.GType.Init ();

                        VolumeMonitor mon = VolumeMonitor.Default;

                        mon.MountAdded += delegate(object o, MountAddedArgs a) {
                                Console.WriteLine ("Mount Added: {0}", a.Mount.Name);
                                loop.Quit ();
                        };
                        mon.MountRemoved += delegate(object o, MountRemovedArgs a) {
                                Console.WriteLine ("Mount removed: {0}", a.Mount.Name);
                                loop.Quit ();
                        };

                        loop = new GLib.MainLoop ();
                        loop.Run ();
                }
        }
}

_______________________________________________
Gtk-sharp-list maillist  -  Gtk-sharp-list@...
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list

Parent Message unknown GIO# System.InvalidCastException in VolumeMonitor.MountAdded event

by Chris Szikszoy :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Christian,

Thanks for the patch.  I gave it a try but am getting a different
exception now (with your test program).  I'm getting an
System.IndexOutOfRangeException.  From the test program, a.Args.Length
returns only 1, and I see that the generated code (from MD's assembly
browser now shows:
       get {
               return
GLib.MountAdapter.GetObject(GLib.SignalArgs.get_Args()[1]);

 unknown opcode: Isinst
       }

I was, however, able to finally get the Mount from the event by doing this:
       Mount m = GLib.MountAdapter.GetObject (a.Args [0] as GLib.Object);
       Console.WriteLine (m.Name);

Thanks again, I'll be more than happy to test any other patches you
have as well.

Chris S.
(cszikszoy on Freenode & GimpNet)

On Sun, Oct 11, 2009 at 2:19 AM, Christian Hoff <christian_hoff@...> wrote:

> Chris Szikszoy wrote:
>>
>> I saw that GIO# bindings landed in the mainline gtk# branch recently and
>> wanted experiment to see if I could replace the old Gnome.VFS in my
>> application.  I've built the latest gtk# from svn and am running into a
>> problem with the VolumeMonitor.Mount* signals.  I keep getting an
>> InvalidCastException when trying to access args.Mount from any of the
>> events
>> (MountAdded, MountRemoved, MountPreUnmount, MountChanged).
>>
>> I've looked at the generated code and it seems to be failing at here:
>> public Mount Mount{
>>        get {
>>                return (GLib.Mount)GLib.SignalArgs.get_Args()[0];
>>        }
>> }
>>
>
> I have attached a patch which fixes the issue together with a modified
> version of your test program. Mike, could you review?
>
>
> Christian
>
>
_______________________________________________
Gtk-sharp-list maillist  -  Gtk-sharp-list@...
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list

Re: GIO# System.InvalidCastException in VolumeMonitor.MountAdded event

by Christian Hoff :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Chris Szikszoy wrote:

> Hi Christian,
>
> Thanks for the patch.  I gave it a try but am getting a different
> exception now (with your test program).  I see that the generated code (from MD's assembly
> browser now shows:
>        get {
>                return
> GLib.MountAdapter.GetObject(GLib.SignalArgs.get_Args()[1]);
>        }
>  
You have applied the patch against the current stable version (2.12) of
the code generator. If you apply it to the Gtk# trunk generator,
everything will work just fine.
> I was, however, able to finally get the Mount from the event by doing this:
>        Mount m = GLib.MountAdapter.GetObject (a.Args [0] as GLib.Object);
>        Console.WriteLine (m.Name);
>  
Yeah, when I backport this patch, I will need to fix the array indices
as well. Great to see that you got it to work :-) . I'll commit when
we've got Mike's approval.


Christian
_______________________________________________
Gtk-sharp-list maillist  -  Gtk-sharp-list@...
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list