1
0
mirror of https://github.com/Tha14/toxic.git synced 2024-11-23 01:53:02 +01:00

Fix two memory leaks and properly clean up friendlist window

This commit is contained in:
jfreegman 2018-10-09 14:21:33 -04:00
parent ce6d4861fb
commit d18cc8cbc2
No known key found for this signature in database
GPG Key ID: 3627F3144076AE63
4 changed files with 27 additions and 25 deletions

View File

@ -113,11 +113,9 @@ static void realloc_blocklist(int n)
Blocked.index = b_idx; Blocked.index = b_idx;
} }
void kill_friendlist(void) void kill_friendlist(ToxWindow *self)
{ {
int i; for (int i = 0; i < Friends.max_idx; ++i) {
for (i = 0; i < Friends.max_idx; ++i) {
if (Friends.list[i].active && Friends.list[i].group_invite.key != NULL) { if (Friends.list[i].active && Friends.list[i].group_invite.key != NULL) {
free(Friends.list[i].group_invite.key); free(Friends.list[i].group_invite.key);
} }
@ -125,6 +123,8 @@ void kill_friendlist(void)
realloc_blocklist(0); realloc_blocklist(0);
realloc_friends(0); realloc_friends(0);
free(self->help);
del_window(self);
} }
/* Saves the blocklist to path. If there are no items in the blocklist the /* Saves the blocklist to path. If there are no items in the blocklist the

View File

@ -85,7 +85,7 @@ ToxWindow new_friendlist(void);
void disable_chatwin(uint32_t f_num); void disable_chatwin(uint32_t f_num);
int get_friendnum(uint8_t *name); int get_friendnum(uint8_t *name);
int load_blocklist(char *data); int load_blocklist(char *data);
void kill_friendlist(void); void kill_friendlist(ToxWindow *self);
void friendlist_onFriendAdded(ToxWindow *self, Tox *m, uint32_t num, bool sort); void friendlist_onFriendAdded(ToxWindow *self, Tox *m, uint32_t num, bool sort);
Tox_User_Status get_friend_status(uint32_t friendnumber); Tox_User_Status get_friend_status(uint32_t friendnumber);
Tox_Connection get_friend_connection_status(uint32_t friendnumber); Tox_Connection get_friend_connection_status(uint32_t friendnumber);

View File

@ -113,6 +113,21 @@ static const char group_cmd_list[AC_NUM_GROUP_COMMANDS][MAX_CMDNAME_SIZE] = {
#endif /* PYTHON */ #endif /* PYTHON */
}; };
static void kill_groupchat_window(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
log_disable(ctx->log);
line_info_cleanup(ctx->hst);
delwin(ctx->linewin);
delwin(ctx->history);
delwin(ctx->sidebar);
free(ctx->log);
free(ctx);
free(self->help);
del_window(self);
}
int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t type) int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t type)
{ {
if (groupnum > MAX_GROUPCHAT_NUM) { if (groupnum > MAX_GROUPCHAT_NUM) {
@ -120,9 +135,8 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t typ
} }
ToxWindow self = new_group_chat(m, groupnum); ToxWindow self = new_group_chat(m, groupnum);
int i;
for (i = 0; i <= max_groupchat_index; ++i) { for (int i = 0; i <= max_groupchat_index; ++i) {
if (!groupchats[i].active) { if (!groupchats[i].active) {
groupchats[i].chatwin = add_window(m, self); groupchats[i].chatwin = add_window(m, self);
groupchats[i].active = true; groupchats[i].active = true;
@ -140,24 +154,11 @@ int init_groupchat_win(ToxWindow *prompt, Tox *m, uint32_t groupnum, uint8_t typ
} }
} }
kill_groupchat_window(&self);
return -1; return -1;
} }
static void kill_groupchat_window(ToxWindow *self)
{
ChatContext *ctx = self->chatwin;
log_disable(ctx->log);
line_info_cleanup(ctx->hst);
delwin(ctx->linewin);
delwin(ctx->history);
delwin(ctx->sidebar);
free(ctx->log);
free(ctx);
free(self->help);
del_window(self);
}
void free_groupchat(ToxWindow *self, Tox *m, uint32_t groupnum) void free_groupchat(ToxWindow *self, Tox *m, uint32_t groupnum)
{ {
free(groupchats[groupnum].name_list); free(groupchats[groupnum].name_list);

View File

@ -667,7 +667,7 @@ void kill_all_windows(Tox *m)
{ {
size_t i; size_t i;
for (i = 0; i < MAX_WINDOWS_NUM; ++i) { for (i = 2; i < MAX_WINDOWS_NUM; ++i) {
if (windows[i].is_chat) { if (windows[i].is_chat) {
kill_chat_window(&windows[i], m); kill_chat_window(&windows[i], m);
} else if (windows[i].is_groupchat) { } else if (windows[i].is_groupchat) {
@ -675,6 +675,7 @@ void kill_all_windows(Tox *m)
} }
} }
kill_prompt_window(prompt); /* TODO: use enum instead of magic indices */
kill_friendlist(); kill_friendlist(&windows[1]);
kill_prompt_window(&windows[0]);
} }