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.
This commit is contained in:
Nicholas Marriott 2009-07-17 12:12:54 +00:00
parent fc17ce0150
commit 89543e999d
6 changed files with 28 additions and 22 deletions

View File

@ -1,4 +1,4 @@
/* $Id: cmd-choose-session.c,v 1.8 2009-07-14 06:43:32 nicm Exp $ */
/* $Id: cmd-choose-session.c,v 1.9 2009-07-17 12:12:54 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -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);
}

View File

@ -1,4 +1,4 @@
/* $Id: cmd-choose-window.c,v 1.10 2009-07-14 06:43:32 nicm Exp $ */
/* $Id: cmd-choose-window.c,v 1.11 2009-07-17 12:12:54 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -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);
}

View File

@ -1,4 +1,4 @@
/* $Id: cmd-find-window.c,v 1.11 2009-07-14 06:43:32 nicm Exp $ */
/* $Id: cmd-find-window.c,v 1.12 2009-07-17 12:12:54 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -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);
}

View File

@ -1,4 +1,4 @@
/* $Id: server-fn.c,v 1.73 2009-07-17 09:28:27 nicm Exp $ */
/* $Id: server-fn.c,v 1.74 2009-07-17 12:12:54 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>

4
tmux.h
View File

@ -1,4 +1,4 @@
/* $Id: tmux.h,v 1.367 2009-07-17 09:26:21 nicm Exp $ */
/* $Id: tmux.h,v 1.368 2009-07-17 12:12:54 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -1489,7 +1489,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);

View File

@ -1,4 +1,4 @@
/* $Id: window-choose.c,v 1.16 2009-06-25 16:21:32 nicm Exp $ */
/* $Id: window-choose.c,v 1.17 2009-07-17 12:12:54 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -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");