summaryrefslogtreecommitdiff
path: root/web/web_scoreboard.c
diff options
context:
space:
mode:
authorDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-06-02 02:43:58 -0700
committerDavid Vazgenovich Shakaryan <dvshakaryan@gmail.com>2022-06-02 02:43:58 -0700
commit823b81363a1145de4b8660153007b11faf4e2c56 (patch)
tree09d9c70e232c1b98ec8447cae6b6618bcd91497d /web/web_scoreboard.c
parent1db534799ef5b41114d80fce0c38f1c93235a8a3 (diff)
downloaddartboat-823b81363a1145de4b8660153007b11faf4e2c56.tar.gz
dartboat-823b81363a1145de4b8660153007b11faf4e2c56.tar.xz
web: fix minor mem leaks and bugs
Diffstat (limited to 'web/web_scoreboard.c')
-rw-r--r--web/web_scoreboard.c28
1 files changed, 23 insertions, 5 deletions
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");