From 216df07688c3811983d4e3430ad59689ebaab889 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Fri, 17 Jul 2009 07:05:58 +0000 Subject: [PATCH] A similar for fix for window_choose: don't rely on the callback always being called to free data, have a separate free callback and call it from the mode cleanup code. --- cmd-choose-session.c | 3 +-- cmd-choose-window.c | 3 +-- cmd-find-window.c | 3 +-- tmux.h | 2 +- window-choose.c | 27 ++++++++++++++++++--------- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/cmd-choose-session.c b/cmd-choose-session.c index 22f7d04b..09c8216d 100644 --- a/cmd-choose-session.c +++ b/cmd-choose-session.c @@ -84,7 +84,7 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx) cdata->client = server_client_index(ctx->curclient); window_choose_ready( - wl->window->active, cur, cmd_choose_session_callback, cdata); + wl->window->active, cur, cmd_choose_session_callback, xfree, cdata); return (0); } @@ -103,5 +103,4 @@ cmd_choose_session_callback(void *data, int idx) server_redraw_client(c); } } - xfree(cdata); } diff --git a/cmd-choose-window.c b/cmd-choose-window.c index baa55eb2..33487e27 100644 --- a/cmd-choose-window.c +++ b/cmd-choose-window.c @@ -85,7 +85,7 @@ cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx) fatalx("session not found"); window_choose_ready( - wl->window->active, cur, cmd_choose_window_callback, cdata); + wl->window->active, cur, cmd_choose_window_callback, xfree, cdata); return (0); } @@ -102,5 +102,4 @@ cmd_choose_window_callback(void *data, int idx) server_redraw_session(s); recalculate_sizes(); } - xfree(cdata); } diff --git a/cmd-find-window.c b/cmd-find-window.c index 21461cdc..34ea75ec 100644 --- a/cmd-find-window.c +++ b/cmd-find-window.c @@ -140,7 +140,7 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx) fatalx("session not found"); window_choose_ready( - wl->window->active, 0, cmd_find_window_callback, cdata); + wl->window->active, 0, cmd_find_window_callback, xfree, cdata); out: ARRAY_FREE(&list_idx); @@ -161,5 +161,4 @@ cmd_find_window_callback(void *data, int idx) server_redraw_session(s); recalculate_sizes(); } - xfree(cdata); } diff --git a/tmux.h b/tmux.h index 1e8e37a3..0720b25b 100644 --- a/tmux.h +++ b/tmux.h @@ -1490,7 +1490,7 @@ void window_choose_vadd( void printflike3 window_choose_add( struct window_pane *, int, const char *, ...); void window_choose_ready(struct window_pane *, - u_int, void (*)(void *, int), void *); + u_int, void (*)(void *, int), void (*)(void *), void *); /* names.c */ void set_window_names(void); diff --git a/window-choose.c b/window-choose.c index f199b847..a1f1a069 100644 --- a/window-choose.c +++ b/window-choose.c @@ -59,7 +59,8 @@ struct window_choose_mode_data { u_int top; u_int selected; - void (*callback)(void *, int); + void (*callbackfn)(void *, int); + void (*freefn)(void *); void *data; }; @@ -86,8 +87,8 @@ window_choose_add(struct window_pane *wp, int idx, const char *fmt, ...) } void -window_choose_ready(struct window_pane *wp, - u_int cur, void (*callback)(void *, int), void *cdata) +window_choose_ready(struct window_pane *wp, u_int cur, + void (*callbackfn)(void *, int), void (*freefn)(void *), void *cdata) { struct window_choose_mode_data *data = wp->modedata; struct screen *s = &data->screen; @@ -96,7 +97,8 @@ window_choose_ready(struct window_pane *wp, if (data->selected > screen_size_y(s) - 1) data->top = ARRAY_LENGTH(&data->list) - screen_size_y(s); - data->callback = callback; + data->callbackfn = callbackfn; + data->freefn = freefn; data->data = cdata; window_choose_redraw_screen(wp); @@ -109,7 +111,11 @@ window_choose_init(struct window_pane *wp) struct screen *s; wp->modedata = data = xmalloc(sizeof *data); - data->callback = NULL; + + data->callbackfn = NULL; + data->freefn = NULL; + data->data = NULL; + ARRAY_INIT(&data->list); data->top = 0; @@ -131,6 +137,9 @@ window_choose_free(struct window_pane *wp) struct window_choose_mode_data *data = wp->modedata; u_int i; + if (data->freefn != NULL && data->data != NULL) + data->freefn(data->data); + mode_key_free(&data->mdata); for (i = 0; i < ARRAY_LENGTH(&data->list); i++) @@ -168,12 +177,12 @@ window_choose_key(struct window_pane *wp, unused struct client *c, int key) switch (mode_key_lookup(&data->mdata, key)) { case MODEKEYCMD_QUIT: - data->callback(data->data, -1); + data->callbackfn(data->data, -1); window_pane_reset_mode(wp); break; case MODEKEYCMD_CHOOSE: item = &ARRAY_ITEM(&data->list, data->selected); - data->callback(data->data, item->idx); + data->callbackfn(data->data, item->idx); window_pane_reset_mode(wp); break; case MODEKEYCMD_UP: @@ -273,7 +282,7 @@ window_choose_mouse(struct window_pane *wp, data->selected = idx; item = &ARRAY_ITEM(&data->list, data->selected); - data->callback(data->data, item->idx); + data->callbackfn(data->data, item->idx); window_pane_reset_mode(wp); } @@ -287,7 +296,7 @@ window_choose_write_line( struct grid_cell gc; int utf8flag; - if (data->callback == NULL) + if (data->callbackfn == NULL) fatalx("called before callback assigned"); utf8flag = options_get_number(&wp->window->options, "utf8");