diff options
author | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2025-02-09 01:32:06 +0330 |
---|---|---|
committer | A404M <ahmadmahmoudiprogrammer@gmail.com> | 2025-02-09 01:32:06 +0330 |
commit | de52585a1b2736a6a788ebc57000d7496f259e64 (patch) | |
tree | 9fdcf25c5074f850f1fab0658efee0ae7f95599d /src/utils/file.c | |
parent | cb26dd0fc9c1e33be1c32eb85b04434526004e6a (diff) |
fix some memory leak
Diffstat (limited to 'src/utils/file.c')
-rw-r--r-- | src/utils/file.c | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/src/utils/file.c b/src/utils/file.c index f2244bf..89ccfa8 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -4,12 +4,32 @@ #include "utils/memory.h" #include <stddef.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> size_t fileCodes_capacity = 0; char **fileCodes = NULL; -const char **fileCodes_names = 0; +char **fileCodes_names = NULL; size_t fileCodes_length = 0; +void fileInit() { + fileCodes_capacity = 0; + fileCodes = a404m_malloc(fileCodes_capacity * sizeof(*fileCodes)); + fileCodes_names = a404m_malloc(fileCodes_capacity * sizeof(*fileCodes_names)); + fileCodes_length = 0; +} + +void fileDelete() { + fileCodes_capacity = 0; + for (size_t i = 0; i < fileCodes_length; ++i) { + free(fileCodes[i]); + free(fileCodes_names[i]); + } + free(fileCodes); + free(fileCodes_names); + fileCodes_length = 0; +} + char *readWholeFile(const char *filePath) { FILE *file = fopen(filePath, "r"); @@ -36,7 +56,9 @@ char *readWholeFile(const char *filePath) { fileCodes_names, fileCodes_capacity * sizeof(*fileCodes_names)); } fileCodes[fileCodes_length] = str; - fileCodes_names[fileCodes_length] = filePath; + fileCodes_names[fileCodes_length] = + a404m_malloc((strlen(filePath) + 1) * sizeof(**fileCodes_names)); + strcpy(fileCodes_names[fileCodes_length], filePath); fileCodes_length += 1; return str; |