From a043847dde1699410ca479732d2546417e2e5377 Mon Sep 17 00:00:00 2001 From: MoAlyousef Date: Wed, 25 Oct 2023 23:20:19 +0300 Subject: [PATCH] schedule callback deletion --- fltk | 2 +- include/cfl_widget.hpp | 31 ++++++++++++++++++------------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/fltk b/fltk index 0636c0b..06d1289 160000 --- a/fltk +++ b/fltk @@ -1 +1 @@ -Subproject commit 0636c0b3e69c80a11a385ebb7fa3db5c8ff06a42 +Subproject commit 06d12892f90233c4ec50109fc2d2f06c5ae14432 diff --git a/include/cfl_widget.hpp b/include/cfl_widget.hpp index 045d0c0..4271c76 100644 --- a/include/cfl_widget.hpp +++ b/include/cfl_widget.hpp @@ -13,6 +13,11 @@ struct is_same { constexpr static inline bool value = true; }; +struct Deleter { + void (*deleter)(void *d); + void *d[4]; +}; + template struct Widget_Derived : public T { Widget_Derived(const Widget_Derived &) = delete; @@ -90,21 +95,21 @@ struct Widget_Derived : public T { if (deleter2 && deleter_data_) { deleter2(this, deleter_data_); } else if (deleter) { - if (ev_data_) - deleter(ev_data_); - ev_data_ = nullptr; - if (resize_data_) - deleter(resize_data_); - resize_data_ = nullptr; - inner_handler = nullptr; - if (draw_data_) - deleter(draw_data_); - draw_data_ = nullptr; - inner_drawer = nullptr; - if (this->user_data()) - deleter(this->user_data()); + auto user_data = this->user_data(); this->user_data(nullptr); this->callback((void (*)(Fl_Widget *, void *)) nullptr); + void *d[4] = { user_data, this->ev_data_, this->draw_data_, this->resize_data_ }; + auto data = new Deleter{}; + data->deleter = deleter; + memcpy(data->d, d, sizeof(d)); + Fl::add_timeout(0.0001, [](void *d) { + auto w = (Deleter *)d; + for (int i = 0; i < 4; i++) { + if (w->d[i]) + w->deleter(w->d[i]); + } + delete w; + }, data); } } };