Re: [gtk-list] gtk--: bug with widget instantiation?



Christof Petig <christof.petig@wtal.de> writes:
> here's a really strange bug:
> using the following statements (window1.cc:14) I get an SIGSEGV:
> 
> vector <Gtk_MenuItem>  optionmenu1_menuitems;
> 
>         optionmenu1_menuitems.push_back(Gtk_MenuItem("one"));
>         optionmenu1_menuitems.push_back(Gtk_MenuItem("three"));
>         optionmenu1_menuitems.push_back(Gtk_MenuItem("nine"));
>         optionmenu1_menuitems.push_back(Gtk_MenuItem("twentyseven"));
> 
> As I understand the current C++ standards and implementations this would
> correspond to
> 1 - create an temporary MenuItem
> 2 - copy it into the vector
> 3 - destruct the temporary

between steps 2 and 3, there's two gtk-- objects available - should
there be two widgets in the screen or not ? :) Also which instance
gets to override the virtual functions or receive signals? (when gtk
widget's virtual function is called, gtk-- uses gtk_object_get_data to
get the this pointer of "current" C++ object - copying the widget
changes that this pointer... :)

Hmm... I was surpriced the compiler did not give compile time error on
the code. Guess we should provide copy constructor which increases
reference count on the gtk+ object or (preferably) make the copy
constructor private and thus disallowing copying of widgets. Another
thing to look is all pointer members inside widgets and places where
this pointer is passed to gtk. (Gtk_Object::Gtk_Object at least passes
this pointer to the gtk and should be dealt in the copying
operation...)

> Though I feel this is a kind of "don't do that" TM, I would really like
> it working. Since using pointers is tiring.

It might be possible to make copying gtk-- widgets work - but many
things needs to be fixed before that can safely happen. (until that we
should really disallow copying widgets) If proper semantics for the
copying operation can be found, maybe that feature can be implemented,
but... Better use vector<auto_ptr<Gtk_Widget> > instead of
vector<Gtk_Widget> currently. (main problem with using pointers is
destructing the widgets when the vector dies or when the widget is
removed from the vector..)

(oh, I never tried if auto_ptr<> works any better - but it might have chance
of working alittle better :)

-- 
-- Tero Pulkkinen -- terop@modeemi.cs.tut.fi --



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]