Skip to content

Commit

Permalink
VM: free the memory allocated for the stack when the program exits
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Aug 6, 2024
1 parent 7b083a6 commit c8e8bb2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bool Node::operator==(const AbstractSyntaxTree &other) const {
void Node::compile(Program &program, Segment &segment) const {
switch (token.type) {
case String: {
auto string = new StringObject(token.value.size(), (char *) token.value.c_str());
auto string = new StringObject(token.value.size(), strdup(token.value.c_str()));
segment.instructions.push_back(
Instruction{
.type = Instruction::InstructionType::LoadObject,
Expand Down
5 changes: 5 additions & 0 deletions src/vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ VM::VM() {
}
VM::~VM() {
free(stack);
if (pointersStack == nullptr)
return;
for (auto i = 0; i < pointersStackSize; i++)
delete pointersStack[i];
free(pointersStack);
}
inline void VM::newStackFrame(const Segment &segment) {
uint64_t *locals{};
Expand Down

0 comments on commit c8e8bb2

Please sign in to comment.