[Gtk-sharp-list] Change a Gtk.Label in a Gtk.MenuItem

Jonathan Pryor jonpryor@vt.edu
Wed, 01 Oct 2003 07:17:05 -0400

I have seen this as well.

I just haven't gotten around to writing a minimal test case and filing
it in bugzilla.

It appears to be a Gtk# bug, in which GTK+ doesn't like Gtk# accessing
GTK+ objects that Gtk# didn't create.

For example, MenuItem.NewWithLabel creates a MenuItem with a (GTK+
created) Label child.  Attempts to access/modify the child label
generates the error message that you're seeing.

However, I haven't seen your behavior where it works the first time and
fails thereafter.  It always fails for me (IIRC).

Given the GTK+ documentation I've read, this is a perfectly legal (and
common!) thing to do, so I can only conclude it's a Gtk# issue.

 - Jon

On Wed, 2003-10-01 at 03:54, Richard Torkar wrote:
> Hi,
> I'm a bit stunned here so I thought I better ask the developers on the
> list directly and so that the answers to this mail will be archived for
> future reference to help others.
> Q: How do I change the Gtk.Label in a Gtk.MenuItem?
> I have a MenuItem hidden (using .Hide()) in my application.
> When an event is initiated from GConf# it means that the application
> should update the MenuItem by changing the label and do a .Show ().
> if (stringArray [0] != "") { 
> 	/*Show a separator in the menu*/
> 	mySeparator.Show ();
> 	/* It bails here the second time:(*/
> 	foreach (Label l in myMenuItem.Children) 
> 		l.Text = stringArray [0];
> 	myMenuItem.Show ();
> } 
> The strange thing is that it works the _first_ time when the application
> starts. It populates the MenuItem according to the setting in GConf#.
> But the second time, when I use gconf-editor and change a value, thus
> throwing an event, it crashes miserably (output below).
> The line "Label l in myMenuItem.Children" is obviously wrong but how?
> I also tried doing a:
> ((Label) myMenuItem.Child).Text = "foo";
> But that evidently fails since Child is write-only...
> I noticed that Gtk#.Button has a property Label, shouldn't this be the
> case with Gtk#.MenuItem also?
> (I'm well aware that Gtk does not have a gtk_menu_item_set_label)
> Crash output:
> (myApp:5993): GLib-GObject-WARNING **: instance of invalid
> non-instantiatable type `<invalid>'
> (myApp:5993): GLib-GObject-CRITICAL **: file gsignal.c: line 2490
> (g_signal_emit_valist): assertion `G_TYPE_CHECK_INSTANCE (instance)'
> failed
> (myApp:5993): GLib-GObject-WARNING **: instance with invalid (NULL)
> class pointer
> (myApp:5993): GLib-GObject-CRITICAL **: file gsignal.c: line 2056
> (g_signal_handlers_destroy): assertion `G_TYPE_CHECK_INSTANCE
> (instance)' failed
> Unhandled Exception: System.NullReferenceException: A null value was
> found where an object instance was required
> in (unmanaged) (wrapper managed-to-native) GLib.Object:g_object_unref
> (intptr)
> in <0x00004> (wrapper managed-to-native) GLib.Object:g_object_unref
> (intptr)
> in <0x000df> GLib.Object:GetObject (intptr,bool)
> in <0x00178> .ListEnumerator:get_Current ()
> in <0x00148> myApp.Replica.AbstractApp:SetUpMenu (string[])
> in <0x00211> myApp.Replica.TextApp:OnPreferenceChanged
> (object,System.ComponentModel.PropertyChangedEventArgs)
> in <0x0005a> (wrapper delegate-invoke)
> System.MulticastDelegate:invoke_void_object_PropertyChangedEventArgs
> (object,System.ComponentModel.PropertyChangedEventArgs)
> in <0x00046> myApp.Preferences:OnRecentDocumentsNotify
> (object,GConf.NotifyEventArgs)
> in <0x0005a> (wrapper delegate-invoke)
> System.MulticastDelegate:invoke_void_object_NotifyEventArgs
> (object,GConf.NotifyEventArgs)
> in <0x00118> GConf.NotifyWrapper:NotifyCB (intptr,uint,intptr,intptr)
> in <0x00047> (wrapper native-to-managed) GConf.NotifyWrapper:NotifyCB
> (intptr,uint,intptr,intptr)
> in (unmanaged) (wrapper managed-to-native) Gtk.Application:gtk_main ()
> in <0x00004> (wrapper managed-to-native) Gtk.Application:gtk_main ()
> in <0x00007> Gtk.Application:Run ()
> in <0x00007> Gnome.Program:Run ()
> in <0x00195> myApp:Main (string[])
>                                                                                                                                                                       Grateful for any explanation... I'm going nuts.
> /Richard