Fix memory leak by adding "owner" concept of node
This commit is contained in:
parent
bdfc36ea3f
commit
bb6c681780
3 changed files with 17 additions and 9 deletions
|
@ -60,6 +60,7 @@ union hf__node_value {
|
|||
struct hf__node {
|
||||
enum hf__node_type type;
|
||||
union hf__node_value value;
|
||||
bool is_owner;
|
||||
};
|
||||
|
||||
struct hf__parser {
|
||||
|
|
|
@ -7,6 +7,11 @@
|
|||
|
||||
void free_word_def_value(void *x) {
|
||||
struct hf__interpreter__word *word = x;
|
||||
|
||||
for (size_t i = 0; i < word->body_len; i++) {
|
||||
// TODO: free nodes
|
||||
}
|
||||
|
||||
free(word->body);
|
||||
free(word);
|
||||
}
|
||||
|
@ -54,17 +59,12 @@ struct hf__result words__word(struct hf__interpreter *const interpreter,
|
|||
hf__parser__node_array_push(
|
||||
&interpreter->call_stack, &interpreter->call_stack_len,
|
||||
&interpreter->call_stack_size, (*word)->body[i]);
|
||||
interpreter->call_stack[interpreter->call_stack_len - 1].is_owner = false;
|
||||
}
|
||||
|
||||
/* FIXME: Word name points to copy of pointer of original name,
|
||||
when cleared here all other references loose access to the pointer's value
|
||||
=> SIGSEV!
|
||||
|
||||
For now I am going to ignore the memory leak.
|
||||
Copying the word name may be a better option but is pretty memory
|
||||
inefficient.
|
||||
*/
|
||||
// free(node->value.word.value);
|
||||
if (node->is_owner) {
|
||||
free(node->value.word.value);
|
||||
}
|
||||
|
||||
return HF__OK;
|
||||
}
|
||||
|
|
|
@ -145,6 +145,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
|||
(struct hf__node){
|
||||
.type = HF__NODE_TYPE__NUMBER,
|
||||
.value = {.number = negative ? -number : number},
|
||||
.is_owner = true,
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
@ -194,6 +195,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
|||
(struct hf__node){
|
||||
.type = HF__NODE_TYPE__CHAR,
|
||||
.value = {.ch = ch},
|
||||
.is_owner = true,
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
@ -218,6 +220,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
|||
node = (struct hf__node){
|
||||
.type = HF__NODE_TYPE__WORD,
|
||||
.value = {.word = {.hash = hash, .value = lower_s}},
|
||||
.is_owner = true,
|
||||
};
|
||||
} else {
|
||||
node.type = **type_ptr;
|
||||
|
@ -302,6 +305,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
|||
(struct hf__node){
|
||||
.type = HF__NODE_TYPE__WORD_DEF,
|
||||
.value = {.word_def = word_def},
|
||||
.is_owner = true,
|
||||
});
|
||||
|
||||
break;
|
||||
|
@ -323,6 +327,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
|||
src, tokens[i].location.start,
|
||||
tokens[i].location.end),
|
||||
},
|
||||
.is_owner = true,
|
||||
});
|
||||
break;
|
||||
|
||||
|
@ -336,6 +341,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
|||
src, tokens[i].location.start,
|
||||
tokens[i].location.end),
|
||||
},
|
||||
.is_owner = true,
|
||||
});
|
||||
break;
|
||||
|
||||
|
@ -349,6 +355,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
|||
src, tokens[i].location.start,
|
||||
tokens[i].location.end),
|
||||
},
|
||||
.is_owner = true,
|
||||
});
|
||||
break;
|
||||
|
||||
|
|
Loading…
Reference in a new issue