From df6961100c276b854be0c6c1ec9f616f22412df6 Mon Sep 17 00:00:00 2001 From: TimePath Date: Wed, 17 Feb 2016 18:04:04 +1100 Subject: [PATCH] Fix on_key_press lifetime --- libs/uilib/uilib.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/libs/uilib/uilib.cpp b/libs/uilib/uilib.cpp index 7b3fd6e5..191990e0 100644 --- a/libs/uilib/uilib.cpp +++ b/libs/uilib/uilib.cpp @@ -73,15 +73,19 @@ namespace ui { std::uint64_t Window::on_key_press(bool (*f)(Widget widget, ui_evkey *event, void *extra), void *extra) { - auto pass = std::make_tuple(f, extra); - auto func = [](ui_widget *widget, GdkEventKey *event, void *pass_) -> bool { - using pass_t = decltype(pass); - auto &args = *(pass_t *) pass_; - auto func = std::get<0>(args); - auto pass = std::get<1>(args); - return func(Widget(widget), event, pass); + using f_t = decltype(f); + struct user_data { + f_t f; + void *extra; + } *pass = new user_data{f, extra}; + auto dtor = [](user_data *data, GClosure *) { + delete data; }; - return g_signal_connect(G_OBJECT(*this), "key-press-event", (GCallback) +func, &pass); + auto func = [](ui_widget *widget, GdkEventKey *event, user_data *args) -> bool { + return args->f(Widget(widget), event, args->extra); + }; + auto clos = g_cclosure_new(G_CALLBACK(+func), pass, reinterpret_cast(+dtor)); + return g_signal_connect_closure(G_OBJECT(*this), "key-press-event", clos, false); } AccelGroup::AccelGroup() : AccelGroup(GTK_ACCEL_GROUP(gtk_accel_group_new())) -- 2.39.2