From de52585a1b2736a6a788ebc57000d7496f259e64 Mon Sep 17 00:00:00 2001
From: A404M <ahmadmahmoudiprogrammer@gmail.com>
Date: Sun, 9 Feb 2025 01:32:06 +0330
Subject: fix some memory leak

---
 src/utils/file.c | 26 ++++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

(limited to 'src/utils/file.c')

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;
-- 
cgit v1.2.3