From abcb2559fee604bf2e12344f82372ed97e2c6a34 Mon Sep 17 00:00:00 2001 From: winterrdog Date: Mon, 21 Nov 2022 09:13:24 +0300 Subject: [PATCH] fixed memory leaks --- kilo.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/kilo.c b/kilo.c index ba88a97..5b194b5 100644 --- a/kilo.c +++ b/kilo.c @@ -470,9 +470,24 @@ void editorInsertRow(int at, char *s, size_t len) { } void editorFreeRow(erow *row) { - free(row->render); - free(row->chars); - free(row->hl); + if (row) { + for (int i = 0; i < E.numrows; ++i) { + erow* tmp_row = row + i; + + if (tmp_row->chars && tmp_row->render && tmp_row->hl) { + free(tmp_row->render); + free(tmp_row->chars); + free(tmp_row->hl); + + tmp_row->render = NULL; + tmp_row->chars = NULL; + tmp_row->hl = NULL; + } + } + + free(row); + row = NULL; + } } void editorDelRow(int at) { @@ -965,6 +980,14 @@ void editorProcessKeypress() { quit_times--; return; } + + editorFreeRow(E.row); + + if (E.filename) { + free(E.filename); + E.filename = NULL; + } + write(STDOUT_FILENO, "\x1b[2J", 4); write(STDOUT_FILENO, "\x1b[H", 3); exit(0);