summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2025-02-09 08:06:41 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2025-02-09 08:06:41 +0330
commit07cca00060de91569074f61172c9406f01eaefe7 (patch)
treeaa267e6a9adf216dabb523777d9ee379d1785d32
parentcf66b5e2db4424cf1947f47b95d7a02344eb315c (diff)
fix memory leaks
-rw-r--r--Makefile4
-rw-r--r--src/runner/runner.c25
-rw-r--r--src/runner/runner.h2
3 files changed, 27 insertions, 4 deletions
diff --git a/Makefile b/Makefile
index a32da73..bb3df23 100644
--- a/Makefile
+++ b/Makefile
@@ -18,9 +18,9 @@ NC := \033[0m
INC_DIRS := $(SRC_DIR)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
-CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -O3
+# CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -O3
# CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -Oz
-# CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -g
+CFLAGS := $(INC_FLAGS) -Wall -Wextra -std=gnu23 -g
EXEC_FILE := $(BUILD_DIR)/$(PROJECT_NAME)
diff --git a/src/runner/runner.c b/src/runner/runner.c
index 30f90f4..2378387 100644
--- a/src/runner/runner.c
+++ b/src/runner/runner.c
@@ -6,6 +6,17 @@
#include <stdlib.h>
#include <string.h>
+void runnerVariablesDelete(RunnerVariables *variables) {
+ for (size_t i = 0; i < variables->size; ++i) {
+ if (variables->data[i]->value != NULL) {
+ astTreeDelete(variables->data[i]->value);
+ }
+ free(variables->data[i]);
+ }
+ free(variables->data);
+ free(variables);
+}
+
void runnerVariablePush(RunnerVariables *variables, AstTreeVariable *variable) {
size_t variables_size =
a404m_malloc_usable_size(variables->data) / sizeof(*variables->data);
@@ -98,9 +109,16 @@ bool runAstTree(AstTreeRoot *root) {
variable_value->token == AST_TREE_TOKEN_FUNCTION) {
AstTree *main = variable_value;
- return runAstTreeFunction(main, NULL, 0, &pages) == &AST_TREE_VOID_VALUE;
+
+ const bool ret =
+ runAstTreeFunction(main, NULL, 0, &pages) == &AST_TREE_VOID_VALUE;
+ runnerVariablesDelete(pages.data[pages.size - 1]);
+ free(pages.data);
+ return ret;
}
}
+ runnerVariablesDelete(pages.data[pages.size - 1]);
+ free(pages.data);
printLog("main function is not found");
return false;
}
@@ -183,7 +201,7 @@ AstTree *runAstTreeFunction(AstTree *tree, AstTree **arguments,
continue;
case AST_TREE_TOKEN_VARIABLE_DEFINE: {
AstTreeVariable *variable = expr->metadata;
- runnerVariableSetValue(&pages, variable, variable->value);
+ runnerVariableSetValue(&pages, variable, copyAstTree(variable->value));
}
continue;
case AST_TREE_TOKEN_OPERATOR_SUM:
@@ -204,6 +222,9 @@ AstTree *runAstTreeFunction(AstTree *tree, AstTree **arguments,
}
RETURN:
+ runnerVariablesDelete(pages.data[pages.size - 1]);
+ free(pages.data);
+
return ret;
}
diff --git a/src/runner/runner.h b/src/runner/runner.h
index ac36e84..758ed28 100644
--- a/src/runner/runner.h
+++ b/src/runner/runner.h
@@ -18,6 +18,8 @@ typedef struct RunnerVariablePages {
size_t size;
} RunnerVariablePages;
+void runnerVariablesDelete(RunnerVariables *variables);
+
void runnerVariablePush(RunnerVariables *variables,AstTreeVariable *variable);
void runnerVariableSetValue(RunnerVariablePages *pages,AstTreeVariable *variable,AstTree *value);
AstTree* runnerVariableGetValue(RunnerVariablePages *pages,AstTreeVariable *variable);