Re: [java-gnome-hackers] HBox constructor causing segfaults.
Brought to you by:
afcowie
From: Tom B. <Tom...@Su...> - 2002-10-20 17:30:57
|
Yeah, constructors can be evil, since a subclass can't control when its superclass gets called. What many toolkits do to work around this is to define a protected "void init()" which gets called by the superclass's constructor. This allows a subclass like AppBar to do something like: public AppBar() { super(); // initialization moved down... } protected init() { // initialization here super.init(); } The init method is a bit dangerous in that subclassers need to realize that they are dealing with a partially-initialized object, but at least their superclass can finish initialization with one that is. An alternative I prefer is to have a protected constructor and a public static factory method. Constructors can then initialization objects like they are supposed to, and the factory method can ensure that the event handler hookup is called afterwards before a client is given the object. The main drawback to using a static factory method is that it can't support the Java Beans model, and so these components won't plug into a form editor easily. Tom On Sat, 2002-10-19 at 21:18, Philip A. Chapman wrote: > Everyone, > > In the constructor org.gnu.gtk.HBox.HBox(), I commented out the line: > > initializeEventHandlers(); > > The reason is that it causes segfaults when called during the > construction of org.gnu.gnome.AppBar, which extends HBox. AppBar works > fine if I comment out the line in HBox as specified above and call > initializeEventHandlers() from AppBar's constructor AFTER calling > AppBar's gnome_appbar_new native method (thereby obtaining a handle). > > The same applies for org.gnu.gnome.DateEdit as it also extends > org.gnu.gtk.HBox. > > Jeffrey, > You are far more familiar with org_gnu_glib_GObject's create_callback > method than I. Would you mind looking into this? Should subclasses of > HBox simply always call initializeEventHandlers() or is there a way to > make it work from HBox's constructor? > > The good news is that the TestGNOME application pretty much works now. > There's a few annoying things, like buttons that do nothing on the > dialogs that are opened from the main app. However. there are no more > segfaults and all the enabled buttons on the main app seem to be doing > what they are supposed to. > > Thanks, > -- > Philip A. Chapman |