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 {
|
struct hf__node {
|
||||||
enum hf__node_type type;
|
enum hf__node_type type;
|
||||||
union hf__node_value value;
|
union hf__node_value value;
|
||||||
|
bool is_owner;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct hf__parser {
|
struct hf__parser {
|
||||||
|
|
|
@ -7,6 +7,11 @@
|
||||||
|
|
||||||
void free_word_def_value(void *x) {
|
void free_word_def_value(void *x) {
|
||||||
struct hf__interpreter__word *word = 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->body);
|
||||||
free(word);
|
free(word);
|
||||||
}
|
}
|
||||||
|
@ -54,17 +59,12 @@ struct hf__result words__word(struct hf__interpreter *const interpreter,
|
||||||
hf__parser__node_array_push(
|
hf__parser__node_array_push(
|
||||||
&interpreter->call_stack, &interpreter->call_stack_len,
|
&interpreter->call_stack, &interpreter->call_stack_len,
|
||||||
&interpreter->call_stack_size, (*word)->body[i]);
|
&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,
|
if (node->is_owner) {
|
||||||
when cleared here all other references loose access to the pointer's value
|
free(node->value.word.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);
|
|
||||||
|
|
||||||
return HF__OK;
|
return HF__OK;
|
||||||
}
|
}
|
||||||
|
|
|
@ -145,6 +145,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
||||||
(struct hf__node){
|
(struct hf__node){
|
||||||
.type = HF__NODE_TYPE__NUMBER,
|
.type = HF__NODE_TYPE__NUMBER,
|
||||||
.value = {.number = negative ? -number : number},
|
.value = {.number = negative ? -number : number},
|
||||||
|
.is_owner = true,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -194,6 +195,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
||||||
(struct hf__node){
|
(struct hf__node){
|
||||||
.type = HF__NODE_TYPE__CHAR,
|
.type = HF__NODE_TYPE__CHAR,
|
||||||
.value = {.ch = ch},
|
.value = {.ch = ch},
|
||||||
|
.is_owner = true,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -218,6 +220,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
||||||
node = (struct hf__node){
|
node = (struct hf__node){
|
||||||
.type = HF__NODE_TYPE__WORD,
|
.type = HF__NODE_TYPE__WORD,
|
||||||
.value = {.word = {.hash = hash, .value = lower_s}},
|
.value = {.word = {.hash = hash, .value = lower_s}},
|
||||||
|
.is_owner = true,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
node.type = **type_ptr;
|
node.type = **type_ptr;
|
||||||
|
@ -302,6 +305,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
||||||
(struct hf__node){
|
(struct hf__node){
|
||||||
.type = HF__NODE_TYPE__WORD_DEF,
|
.type = HF__NODE_TYPE__WORD_DEF,
|
||||||
.value = {.word_def = word_def},
|
.value = {.word_def = word_def},
|
||||||
|
.is_owner = true,
|
||||||
});
|
});
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -323,6 +327,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
||||||
src, tokens[i].location.start,
|
src, tokens[i].location.start,
|
||||||
tokens[i].location.end),
|
tokens[i].location.end),
|
||||||
},
|
},
|
||||||
|
.is_owner = true,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -336,6 +341,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
||||||
src, tokens[i].location.start,
|
src, tokens[i].location.start,
|
||||||
tokens[i].location.end),
|
tokens[i].location.end),
|
||||||
},
|
},
|
||||||
|
.is_owner = true,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -349,6 +355,7 @@ struct hf__result hf__parse(struct hf__parser *const parser,
|
||||||
src, tokens[i].location.start,
|
src, tokens[i].location.start,
|
||||||
tokens[i].location.end),
|
tokens[i].location.end),
|
||||||
},
|
},
|
||||||
|
.is_owner = true,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue