Re: gtk--: segfault in destructor of spinbutton!

Oliver Freyd <> writes:

> If I look at the source, I see that in the constructor and the set_adjustment
> method a pointer is passed, so I'd say the object belongs to the user,
> either as static object or created be new and to be deleted in a destructor.
> The only problem is with the castitem constructor, where the c object
> is given and there is no c++ object. So maybe one should create (new)
> a Gtk_Adjustment there that corresponds to the c object. A flag could
> tell the destructor to delete it, but not the usual Gtk_Adjustment.
> Also in set_adjustment this "owned" Adjustment should be killed
> before changing the pointer to the new one.

Exactly what I had in mind (thanks for reminding me about deleting it
in set_adjustment, though, I was about to forget it. Then again it's
only 8am :-).

> >OTOH, this means that calling get_adjustment() means you have to
> >delete the result. Not very consistent with the gtk API, this is
> >supposed to be a simple accessor.
> >
> That would'n be so nice, I think people will forget it and get memory leaks...

Which was our first concern.

> >- Or, we could overload both set_adjustment() and the constructor to
> >take Gtk_Adjustment&, and those would set some internal flag which
> >would tell the destructor and get_adjustment() to leave the adjustment
> >member alone ?
> >
> What is the real difference between Gtk_Adjustment* and Gtk_Adjustment& ?
> I mean references are pointers internally...

Internally, yes. But you're more likely to use an object allocated
from the heap through a pointer, and a stack object directly :

Gtk_Adjustment adjustment1;
Gtk_Adjustment *adjustment_ptr;

adjustment_ptr = new Gtk_Adjustment();

set_adjustment(adjustment1); // calls set_adjustment(Gtk_Adjustment&)
set_adjustment(adjustment_ptr); // calls set_adjustment(Gtk_Adjustment*)

But that's not a very satisfactory solution anyway.

> >- Finally, may be the simplest solution would be not to delete anything,
> >but have get_adjustment() be a simple accessor indeed, and simply
> >return adjustment_widget, instead of calling
> >gtk_spin_button_get_adjustment() and wrapping a new Gtk_Adjustment
> >around the result. adjustment_widget would of course be maintained by
> >the constructor and set_adjustment().
> >
> Yeah, I think that's the best, the only thing that remains would be 
> the castitem constructor, and this would be rarely used (only my opinion...)

This should be in the next Gtk-- release.


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