From: TimePath Date: Wed, 17 Feb 2016 07:04:04 +0000 (+1100) Subject: Fix on_key_press lifetime X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=df6961100c276b854be0c6c1ec9f616f22412df6;p=xonotic%2Fnetradiant.git Fix on_key_press lifetime --- 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()))