From 823b81363a1145de4b8660153007b11faf4e2c56 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Thu, 2 Jun 2022 02:43:58 -0700 Subject: web: fix minor mem leaks and bugs --- web/web_scoreboard.c | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) (limited to 'web/web_scoreboard.c') diff --git a/web/web_scoreboard.c b/web/web_scoreboard.c index ed25b33..a5001f7 100644 --- a/web/web_scoreboard.c +++ b/web/web_scoreboard.c @@ -16,11 +16,26 @@ static struct { bufstr name, rem, sugg, avg; } *bufs; +static int n_bufs; #define NUM_SLOTS 2 static int slots[NUM_SLOTS]; static int active_pn; +static void free_bufs() +{ + for (int i = 0; i < n_bufs; ++i) { + bufstr_clear(&bufs[i].name); + bufstr_clear(&bufs[i].rem); + bufstr_clear(&bufs[i].sugg); + bufstr_clear(&bufs[i].avg); + } + + free(bufs); + bufs = NULL; + n_bufs = 0; +} + static void flush_slot(int slot) { int pn = slots[slot - 1]; @@ -212,8 +227,10 @@ void update_player_rem(int pn, int rem) void scoreboard_show_info(int num_players) { - if (bufs) free(bufs); - bufs = calloc(num_players, sizeof(*bufs)); + if (bufs) + free_bufs(); + n_bufs = num_players; + bufs = calloc(n_bufs, sizeof(*bufs)); for (int i = 1; i <= num_players && i <= NUM_SLOTS; ++i) { set_slot_player(i, i); @@ -231,8 +248,8 @@ void scoreboard_show_info(int num_players) void scoreboard_hide_info() { - free(bufs); - bufs = NULL; + if (bufs) + free_bufs(); memset(slots, 0, sizeof(slots)); dom_set_uniq_class(NULL, "visible", ".info-slot .inner"); @@ -379,7 +396,8 @@ void draw_visits() if (state->m->n_players != 1 && state->m->legs[1]->n_visits > n_visits) n_visits = state->m->legs[1]->n_visits; - struct dom_elem **elemv = malloc(7 * (n_visits + 1) * sizeof(*elemv)); + struct dom_elem **elemv = malloc( + (9 + (7 * n_visits)) * sizeof(*elemv)); int elemc = 0; elemv[elemc++] = create_div("", "visit-p1-spacer"); -- cgit v1.2.3-70-g09d2