tmus

TUI Music Player
git clone https://git.sinitax.com/sinitax/tmus
Log | Files | Refs | Submodules | LICENSE | sfeed.txt

commit 019761cb13674f85468188b193e2fa7ff959f47f
parent 43282adc30aadefb563ef26bea1233d4cbdb6007
Author: Louis Burda <quent.burda@gmail.com>
Date:   Sat,  8 Jan 2022 15:04:18 +0100

Closed memory leaks

Diffstat:
M.gitignore | 6++----
Msrc/history.c | 68++++++++++++++++++++++++++++++++------------------------------------
Msrc/history.h | 3+--
Msrc/main.c | 61++++++++++++++++++++++++++++++++++++++++---------------------
Msrc/player.c | 16+++++++++++-----
Msrc/tag.c | 3+++
Msrc/track.c | 15++++++++++-----
7 files changed, 99 insertions(+), 73 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -1,9 +1,7 @@ vgcore* +build tmus -env.sh -*.o todo -build *.gch .cache -log +log* diff --git a/src/history.c b/src/history.c @@ -9,43 +9,47 @@ void history_init(struct history *history) { history->list = LIST_HEAD; - history->query = inputln_init(); - history->cmd = history->query; + history->input = inputln_init(); + history->sel = history->input; } void history_submit(struct history *history) { - /* if chose from history free query */ - if (history->cmd != history->query) { - history_pop(history->query); - inputln_free(history->query); + /* if chose from history free input */ + if (history->sel != history->input) { + link_pop(LINK(history->input)); + inputln_free(history->input); } /* pop first in case already in history */ - history_pop(history->cmd); - history_add(history, history->cmd); + link_pop(LINK(history->sel)); + history_add(history, history->sel); /* create new input buf and add to hist */ - history->query = inputln_init(); - history->cmd = history->query; - history_add(history, history->cmd); + history->input = inputln_init(); + history->sel = history->input; + history_add(history, history->sel); } void history_free(struct history *history) { struct link *iter, *next; - struct inputln *ln; + struct link *ln; + + ln = link_pop(LINK(history->input)); + inputln_free(UPCAST(ln, struct inputln)); - for (iter = history->list.next; iter; iter = next) { + for (iter = history->list.next; iter; ) { next = iter->next; - ln = UPCAST(iter, struct inputln); - free(ln); + inputln_free(UPCAST(iter, struct inputln)); + iter = next; } + history->list = LIST_HEAD; - history->query = NULL; - history->cmd = NULL; + history->input = NULL; + history->sel = NULL; } struct inputln * @@ -60,7 +64,7 @@ history_list_prev(struct inputln *cur, const wchar_t *search) return ln; } - return NULL; + return cur; } struct inputln * @@ -81,20 +85,13 @@ history_list_next(struct inputln *cur, const wchar_t *search) void history_prev(struct history *history) { - history->cmd = history_list_prev(history->cmd, history->query->buf); - if (!history->cmd) history->cmd = history->query; + history->sel = history_list_prev(history->sel, history->input->buf); } void history_next(struct history *history) { - history->cmd = history_list_next(history->cmd, history->query->buf); -} - -void -history_pop(struct inputln *line) -{ - link_pop(&line->link); + history->sel = history_list_next(history->sel, history->input->buf); } void @@ -105,13 +102,11 @@ history_add(struct history *history, struct inputln *line) if (list_len(&history->list) == HISTORY_MAX) { /* pop last item to make space */ - back = link_back(&history->list); - back->prev->next = NULL; - ln = UPCAST(back, struct inputln); - inputln_free(ln); + back = link_pop(link_back(&history->list)); + inputln_free(UPCAST(back, struct inputln)); } - link_append(&history->list, &line->link); + list_push_front(&history->list, LINK(line)); } struct inputln * @@ -141,15 +136,15 @@ inputln_free(struct inputln *ln) } void -inputln_left(struct inputln *cmd) +inputln_left(struct inputln *ln) { - cmd->cur = MAX(0, cmd->cur-1); + ln->cur = MAX(0, ln->cur-1); } void -inputln_right(struct inputln *cmd) +inputln_right(struct inputln *ln) { - cmd->cur = MIN(cmd->len, cmd->cur+1); + ln->cur = MIN(ln->len, ln->cur+1); } void @@ -208,6 +203,7 @@ inputln_copy(struct inputln *dst, struct inputln *src) void inputln_replace(struct inputln *line, const wchar_t *str) { + free(line->buf); line->buf = wcsdup(str); ASSERT(line->buf != NULL); line->len = wcslen(str); diff --git a/src/history.h b/src/history.h @@ -16,7 +16,7 @@ struct inputln { struct history { struct link list; - struct inputln *cmd, *query; + struct inputln *sel, *input; }; void history_init(struct history *history); @@ -28,7 +28,6 @@ void history_prev(struct history *history); void history_next(struct history *history); void history_add(struct history *history, struct inputln *line); -void history_pop(struct inputln *line); struct inputln *inputln_init(void); void inputln_free(struct inputln *ln); diff --git a/src/main.c b/src/main.c @@ -183,6 +183,7 @@ cleanup(int exitcode, void* arg) tui_end(); if (!exitcode) data_save(); + data_free(); player_free(); @@ -319,7 +320,26 @@ data_save(void) void data_free(void) { - /* TODO free track info */ + struct link *track_link; + struct link *tag_link; + struct tag *tag; + + log_info("MAIN: data_free()\n"); + + refs_free(&tracks); + refs_free(&tags_sel); + + while (!list_empty(&tags)) { + tag_link = list_pop_front(&tags); + + tag = UPCAST(tag_link, struct tag); + while (!list_empty(&tag->tracks)) { + track_link = list_pop_front(&tag->tracks); + track_free(UPCAST(track_link, struct ref)->data); + ref_free(UPCAST(track_link, struct ref)); + } + tag_free(tag); + } } void @@ -343,7 +363,6 @@ tracks_load(struct tag *tag) track = track_init(tag->fpath, ent->d_name); - track->tags = LIST_HEAD; ref = ref_init(tag); ASSERT(ref != NULL); list_push_back(&track->tags, LINK(ref)); @@ -363,7 +382,6 @@ tracks_load(struct tag *tag) ASSERT(ref != NULL); list_push_back(&tracks, LINK(ref)); } - } closedir(dir); } @@ -466,6 +484,7 @@ toggle_current_tag(void) struct ref *ref; int in_tags, in_playlist; + if (list_empty(&tags)) return; link = link_iter(tags.next, tag_nav.sel); ASSERT(link != NULL); tag = UPCAST(link, struct tag); @@ -688,19 +707,19 @@ cmd_pane_input(wint_t c) switch (c) { case KEY_ESC: - if (history->cmd == history->query) + if (history->sel == history->input) pane_sel = pane_top_sel; else - history->cmd = history->query; + history->sel = history->input; break; case KEY_LEFT: - inputln_left(history->cmd); + inputln_left(history->sel); break; case KEY_RIGHT: - inputln_right(history->cmd); + inputln_right(history->sel); break; case KEY_CTRL('w'): - inputln_del(history->cmd, history->cmd->cur); + inputln_del(history->sel, history->sel->cur); break; case KEY_UP: history_next(history); @@ -709,15 +728,15 @@ cmd_pane_input(wint_t c) history_prev(history); break; case KEY_ENTER: - if (!*history->cmd->buf) { + if (!*history->sel->buf) { pane_sel = pane_top_sel; break; } if (cmd_mode == IMODE_EXECUTE) { - run_cmd(history->cmd->buf); + run_cmd(history->sel->buf); } else if (cmd_mode == IMODE_SEARCH) { - play_track(history->cmd->buf); + play_track(history->sel->buf); } history_submit(history); @@ -725,32 +744,32 @@ cmd_pane_input(wint_t c) break; case KEY_TAB: case KEY_BTAB: - if (history->cmd != history->query) { - inputln_copy(history->query, history->cmd); - history->cmd = history->query; + if (history->sel != history->input) { + inputln_copy(history->input, history->sel); + history->sel = history->input; } if (completion_reset) - inputln_copy(&completion_query, history->query); + inputln_copy(&completion_query, history->input); res = completion(completion_query.buf, c == KEY_TAB, completion_reset); - if (res) inputln_replace(history->query, res); + if (res) inputln_replace(history->input, res); free(res); completion_reset = 0; break; case KEY_BACKSPACE: - if (history->cmd->cur == 0) { + if (history->sel->cur == 0) { pane_sel = pane_top_sel; break; } - inputln_del(history->cmd, 1); + inputln_del(history->sel, 1); completion_reset = 1; break; default: if (!iswprint(c)) return 0; - inputln_addch(history->cmd, c); + inputln_addch(history->sel, c); completion_reset = 1; break; } @@ -841,8 +860,8 @@ cmd_pane_vis(struct pane *pane, int sel) /* cmd and search input */ line = linebuf; - cmd = history->cmd; - if (cmd != history->query) { + cmd = history->sel; + if (cmd != history->input) { index = 0; iter = history->list.next; for (; iter; iter = iter->next, index++) diff --git a/src/player.c b/src/player.c @@ -92,8 +92,12 @@ player_free(void) if (!player->conn) return; - refs_free(&player->history); refs_free(&player->queue); + refs_free(&player->history); + + refs_free(&player->playlist); + + player_clear_msg(); //mpd_run_clear(player->conn); mpd_connection_free(player->conn); @@ -142,7 +146,8 @@ player_update(void) status = mpd_run_status(player->conn); ASSERT(status != NULL); - if (!mpd_run_current_song(player->conn)) { + song = mpd_run_current_song(player->conn); + if (!song) { /* if autoplay and another track just finished, * or there are tracks in queue to be played */ if (player->track && player->autoplay @@ -150,8 +155,9 @@ player_update(void) player->action = PLAYER_ACTION_PLAY_NEXT; } } + mpd_song_free(song); - free(status); + mpd_status_free(status); if (player->action != PLAYER_ACTION_NONE) { handle_mpd_status(mpd_run_clear(player->conn)); @@ -199,13 +205,13 @@ player_update(void) player->loaded = true; player->time_pos = mpd_status_get_elapsed_time(status); player->time_end = mpd_song_get_duration(song); - mpd_song_free(song); } else { - player->track = NULL; player->loaded = false; + player->track = NULL; player->time_pos = 0; player->time_end = 0; } + mpd_song_free(song); switch (mpd_status_get_state(status)) { case MPD_STATE_PAUSE: diff --git a/src/tag.c b/src/tag.c @@ -34,5 +34,8 @@ tag_free(struct tag *tag) free(tag->fname); free(tag->fpath); free(tag->name); + refs_free(&tag->tracks); + + free(tag); } diff --git a/src/track.c b/src/track.c @@ -1,3 +1,4 @@ +#include "ref.h" #include "track.h" #include <wchar.h> @@ -12,21 +13,21 @@ track_init(const char *dir, const char *fname) struct stat info; track = malloc(sizeof(struct track)); - ASSERT(track != NULL); + track->fname = strdup(fname); ASSERT(track->fname != NULL); + track->fpath = aprintf("%s/%s", dir, fname); ASSERT(track->fpath != NULL); + track->name = calloc(strlen(track->fname) + 1, sizeof(wchar_t)); ASSERT(track->name != NULL); mbstowcs(track->name, track->fname, strlen(track->fname) + 1); - if (!stat(track->fpath, &info)) { + track->fid = -1; + if (!stat(track->fpath, &info)) track->fid = info.st_ino; - } else { - track->fid = -1; - } track->tags = LIST_HEAD; @@ -39,4 +40,8 @@ track_free(struct track *t) free(t->fname); free(t->fpath); free(t->name); + + refs_free(&t->tags); + + free(t); }