[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gtkmm
Subject:    [gtkmm] reference counting and option menu swapping
From:       Ben Liblit <liblit () acm ! org>
Date:       2001-08-24 1:00:13
[Download RAW message or body]

I'm using gtk-- with libglade.  No libglade-- just yet, though that will 
probably be next.  I'm a bit confused about reference count management 
for option menus.  I have a solution that works, but it is so ugly that 
it just can't be the "right" way to do things.  I'm hoping someone can 
give me a better recipe.

I have an option menu (with an affiliated menu) created by libglade. 
I've wrapped the option menu up into a Gtk::OptionMenu as follows:

   Gtk::OptionMenu * const option =
       Gtk::wrap( GTK_OPTION_MENU( glade_xml_get_widget( ... ) ) );

Suppose I want to manipulate two underlying menus.  There's the one 
built by libglade, which is already sitting under the option menu. 
Assume I build a second one "manually", at runtime.  Thus:

   Gtk::Menu * const menu1 = option->get_menu();
   Gtk::Menu menu2;

Now suppose I want to swap out menu1 and use menu2 instead, and then 
later I want to put menu1 back.  The following crashes with a 
segmentation fault:

   option->set_menu( menu2 );
   option->set_menu( menu1 );        // segfault here

That's not surprising, because the analogous gtk+ code also crashes: 
when menu2 was installed, the reference count for menu1 dropped to zero 
and it was destroyed.  If I temporarily bump menu1's reference count 
just like I would do in gtk+, everything works:

   gtk_object_ref( static_cast< Gtk::Object * >( menu1 )->gtkobj() );
   option->set_menu( menu2 );
   option->set_menu( menu1 );        // works fine, no crash
   gtk_object_unref( static_cast< Gtk::Object * >( menu1 )->gtkobj() );

But somehow this just doesn't feel right.  gtk-- usually does a 
wonderful job of wrapping and abstracting gtk+, and breaking that 
abstraction just so I can tweak reference counts feels clumsy and wrong. 
  Yet I cannot find any way to do this within the gtk+ framework.  At 
first SigC::ObjectReferenced::reference() and 
SigC::ObjectReferenced::unreference() looked like good candidates, but 
apparently they're not intended for this sort of thing:

   menu1->reference();               // disallowed: protected method
   option->set_menu( menu2 );
   option->set_menu( menu1 );
   menu1->unreference();             // disallowed: protected method

So how *should* I be doing this?  Is there a more elegant way?

I suppose I could encapsulate the gtk_object_ref() and 
gtk_object_unref() calls into the constructor and destructor of a new 
class whose sole purpose is to temporarily bump a Gtk::Object's 
reference count.  That would feel a little cleaner, anyway.  But I don't 
want to do that if there is already a better solution.

Any ideas?


_______________________________________________
to unsubscribe or change your subscription parameters :
http://lists.sourceforge.net/lists/listinfo/gtkmm-main

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic