updates the free function

This commit is contained in:
SuperNovaa41 2025-01-16 09:16:41 -05:00
parent 7f608c0013
commit 55914bad9f
2 changed files with 21 additions and 4 deletions

View File

@ -105,12 +105,12 @@ int screen_buffer_append(const char* in, size_t len);
void screen_buffer_free(screen_buffer_t* buf); void screen_buffer_free(screen_buffer_t* buf);
/** /**
* # free_editor_row * # free_editor_rows
* *
* Frees all of the strings in each row * Frees all of the strings in each row
* and the row array * and the row array
*/ */
void free_editor_row(void); void free_editor_rows(void);
void editor_render_row(row_t* row); void editor_render_row(row_t* row);

View File

@ -156,19 +156,36 @@ void screen_buffer_free(screen_buffer_t *buf)
free(buf->text); free(buf->text);
} }
void free_editor_row(void) void free_row(row_t* row)
{
if (row->line != NULL)
free(row->line);
if (row->render != NULL)
free(row->render);
}
void free_editor_rows(void)
{ {
size_t i; size_t i;
for (i = 0; i < editor.num_rows; i++) for (i = 0; i < editor.num_rows; i++)
free(editor.rows[i].line); free_row(&editor.rows[i]);
free(editor.rows); free(editor.rows);
} }
void editor_render_row(row_t* row) void editor_render_row(row_t* row)
{ {
size_t i, idx;
if (row->render != NULL) // if the row is malloc'd we want to delete it if (row->render != NULL) // if the row is malloc'd we want to delete it
free(row->render); free(row->render);
row->render = malloc(sizeof(char) * row->len); row->render = malloc(sizeof(char) * row->len);
idx = 0;
for (i = 0; i < row->len; i++)
row->render[idx++] = row->line[i];
row->render[idx] = '\0';
row->r_len = idx;
} }
// small helper function to initialize a row // small helper function to initialize a row