On 14 Dec 2009, at 18:23, David Lopez wrote:
> Hi, I am migrating an application from FLTK 1.1.3 to FLTK 1.1.9.
> The problem is the following
> I have a button whose callback deletes the button itself.
>
> It works fine in 1.1.3, since the code for Fl_Widget::do_callback
> is the following:
> void do_callback() {callback_(this,user_data_);}
>
> But in 1.1.9 that code changed to:
> void do_callback() {callback_(this,user_data_); if (callback_ !=
> default_callback) clear_changed();}
>
> So, the instruction clear_changed() executes after the object was
> destroyed, causing a crash.
>
> The question is, isn't valid to delete a widget in its callback?
No, it is never safe to delete a widget inside its own callback.
It never was, but due to some odd quirks it used to sort of work,
most of the time.
> In such case, what would you suggest doing?
Use the documented method: Fl::delete_widget(Fl_Widget*);
Quoting the docs:
Schedules a widget for deletion at the next call to the event loop.
Use this method to delete a widget inside a callback function. As
with normal widget deletion it is your responsibility to remove() the
widget from any parent group or window before calling this.
To avoid early deletion of widgets, this function should be called
toward the end of a callback and only after any call to the event
loop (Fl::wait(), Fl::flush(), Fl::check(), fl_ask(), etc.).
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk