From 49b1a8a20dd737d65fa3e8257bc3595f0d14dc20 Mon Sep 17 00:00:00 2001 From: Nick <120306885+call-nick@users.noreply.github.com> Date: Tue, 29 Apr 2025 23:17:01 +0300 Subject: [PATCH 1/5] fix: prevent memory leak by freeing directions array --- minecraft.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/minecraft.c b/minecraft.c index 2e21828..66faef6 100644 --- a/minecraft.c +++ b/minecraft.c @@ -164,6 +164,15 @@ vect** init_directions(vect2 view) { return dir; } +void free_directions(vect** directions) { + if (!directions) return; + + for (int y = 0; y < Y_PIXELS; y++) { + free(directions[y]); + } + free(directions); +} + int ray_outside(vect pos) { if (pos.x >= X_BLOCKS || pos.y >= Y_BLOCKS || pos.z >= Z_BLOCKS || pos.x < 0 || pos.y < 0 || pos.z < 0) { @@ -238,6 +247,7 @@ char** get_picture(char** picture, player_pos_view posview, char*** blocks) { picture[y][x] = raytrace(posview.pos, directions[y][x], blocks); } } + free_directions(directions); } void draw_ascii(char** picture) { @@ -427,4 +437,4 @@ int main() { } restore_terminal(); return 0; -} \ No newline at end of file +} From 9e95824102dfe5da69fc03e1e3d810531d51f60d Mon Sep 17 00:00:00 2001 From: Nick <120306885+call-nick@users.noreply.github.com> Date: Tue, 29 Apr 2025 23:18:56 +0300 Subject: [PATCH 2/5] fix: change get_picture() return type to void Function doesn't return anything --- minecraft.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minecraft.c b/minecraft.c index 66faef6..14f127d 100644 --- a/minecraft.c +++ b/minecraft.c @@ -240,7 +240,7 @@ char raytrace(vect pos, vect dir, char*** blocks) { return ' '; } -char** get_picture(char** picture, player_pos_view posview, char*** blocks) { +void get_picture(char** picture, player_pos_view posview, char*** blocks) { vect** directions = init_directions(posview.view); for (int y = 0; y < Y_PIXELS; y++) { for (int x = 0; x < X_PIXELS; x++) { From d1f8919e8376e08865c5748baa72247df7994eb6 Mon Sep 17 00:00:00 2001 From: Nick <120306885+call-nick@users.noreply.github.com> Date: Tue, 29 Apr 2025 23:53:12 +0300 Subject: [PATCH 3/5] fix: prevent memory leak by freeing blocks array --- minecraft.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/minecraft.c b/minecraft.c index 14f127d..5b5579b 100644 --- a/minecraft.c +++ b/minecraft.c @@ -91,6 +91,19 @@ char*** init_blocks() { return blocks; } +void free_blocks(char*** blocks) { + if (!blocks) return; + + for (int i = 0; i < Z_BLOCKS; i++) { + if (!blocks[i]) continue; + for (int j = 0; j < Y_BLOCKS; j++) { + free(blocks[i][j]); + } + free(blocks[i]); + } + free(blocks); +} + player_pos_view init_posview() { player_pos_view posview; posview.pos.x = 5; @@ -405,6 +418,7 @@ int main() { while (1) { process_input(); if (is_key_pressed('q')) { + free_blocks(blocks); exit(0); } update_pos_view(&posview, blocks); @@ -436,5 +450,6 @@ int main() { usleep(20000); } restore_terminal(); + free_blocks(blocks); return 0; } From 5518ce58b5f0d2fc3f4f6ca820ff0f2c060dbeac Mon Sep 17 00:00:00 2001 From: Nick <120306885+call-nick@users.noreply.github.com> Date: Tue, 29 Apr 2025 23:59:38 +0300 Subject: [PATCH 4/5] fix: prevent memory leak by freeing picture array --- minecraft.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/minecraft.c b/minecraft.c index 5b5579b..f93ace7 100644 --- a/minecraft.c +++ b/minecraft.c @@ -77,6 +77,15 @@ char** init_picture() { return picture; } +void free_picture(char** picture) { + if (!picture) return; + + for (int i = 0; i < Y_PIXELS; i++) { + free(picture[i]); + } + free(picture); +} + char*** init_blocks() { char*** blocks = malloc(sizeof(char**) * Z_BLOCKS); for (int i = 0; i < Z_BLOCKS; i++) { @@ -419,6 +428,7 @@ int main() { process_input(); if (is_key_pressed('q')) { free_blocks(blocks); + free_picture(picture); exit(0); } update_pos_view(&posview, blocks); @@ -451,5 +461,6 @@ int main() { } restore_terminal(); free_blocks(blocks); + free_picture(picture); return 0; } From 1a633c4b1102165b09a8a29695f8c49008ff4921 Mon Sep 17 00:00:00 2001 From: Nick <120306885+call-nick@users.noreply.github.com> Date: Wed, 30 Apr 2025 00:03:49 +0300 Subject: [PATCH 5/5] fix: restore terminal state on exit --- minecraft.c | 1 + 1 file changed, 1 insertion(+) diff --git a/minecraft.c b/minecraft.c index f93ace7..b1d59fb 100644 --- a/minecraft.c +++ b/minecraft.c @@ -429,6 +429,7 @@ int main() { if (is_key_pressed('q')) { free_blocks(blocks); free_picture(picture); + restore_terminal(); exit(0); } update_pos_view(&posview, blocks);