fix a bunch of memory bugs

This commit is contained in:
2026-07-04 19:59:00 +10:00
parent 5337a9e375
commit a913b3b24a
5 changed files with 17 additions and 8 deletions

View File

@@ -82,18 +82,24 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
PRINT: {
for (GroundSize i = 0; i < instruction->args.len; i++) {
printValue(HEAP_GET(heap, instruction->args.at[i]));
printf(" ");
}
return CONTINUE;
}
PRINTLN: {
for (GroundSize i = 0; i < instruction->args.len; i++) {
printValue(HEAP_GET(heap, instruction->args.at[i]));
printf(" ");
}
printf("\n");
return CONTINUE;
}
SET: {
HEAP_SET(heap, instruction->args.at[0], *HEAP_GET(heap, instruction->args.at[1]));
if (instruction->args.at[0] != instruction->args.at[1]) {
GroundBytecodeValue copy = Ground.Copy.BytecodeValue(HEAP_GET(heap, instruction->args.at[1]));
Ground.Free.BytecodeValue(&heap->heap[instruction->args.at[0]]);
HEAP_SET(heap, instruction->args.at[0], copy);
}
return CONTINUE;
}
GETTYPE: {
@@ -179,6 +185,7 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
}
sprintf(buf, "%s%s", left->as.String.cstr, right->as.String.cstr);
*final = Ground.New.BytecodeValue(Ground.New.Value.String(Ground.New.String(buf)));
free(buf);
break;
}
default: {
@@ -666,7 +673,6 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
return CONTINUE;
}
TOSTRING: {
GroundBytecodeValue* final = HEAP_GET(heap, instruction->args.at[1]);
GroundBytecodeValue* in = HEAP_GET(heap, instruction->args.at[0]);
char buf[4096];
@@ -689,7 +695,6 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
break;
}
case GroundType_String: {
*in = Ground.New.BytecodeValue(Ground.New.Value.String(final->as.String));
return CONTINUE;
}
@@ -785,8 +790,8 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
// Now we run the thingy
GroundBytecodeValue result = Ground.Bytecode.Program.execute(function->program.ground, &newHeap);
// And store the result
HEAP_SET(heap, instruction->args.at[instruction->args.len - 1], result);
// Deep copy the result before freeing the function's heap
GroundBytecodeValue resultCopy = Ground.Copy.BytecodeValue(&result);
// Clean up the new heap
for (GroundSize i = 0; i < newHeap.len; i++) {
@@ -794,6 +799,9 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
}
free(newHeap.heap);
// And store the result
HEAP_SET(heap, instruction->args.at[instruction->args.len - 1], resultCopy);
return CONTINUE;
}
CALLMETHOD: {

View File

@@ -4,5 +4,6 @@
void _GroundFreeString(GroundString* in) {
free(in->cstr);
in->cstr = NULL;
in->len = 0;
}

View File

@@ -238,7 +238,7 @@ GroundBytecode _GroundNewBytecode(GroundProgram* program, GroundState* state) {
if (Ground.Flags.error) return bytecode;
// Allocate heap
bytecode.heap.heap = malloc(sizeof(GroundValue) * size);
bytecode.heap.heap = malloc(sizeof(GroundBytecodeValue) * size);
if (bytecode.heap.heap == NULL) {
Ground.Log.Error("malloc failed in Ground.New.Bytecode");
Ground.Flags.error = true;

View File

@@ -109,7 +109,7 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
void _GroundBytecodeSave(GroundBytecode* bytecode, const char* path);
GroundBytecode _GroundBytecodeLoad(const char* path);
void _GroundBytecodeHeapSet(GroundBytecodeHeap* heap, GroundSize idx, GroundValue value);
void _GroundBytecodeHeapSet(GroundBytecodeHeap* heap, GroundSize idx, GroundBytecodeValue value);
GroundValue* _GroundBytecodeHeapGet(GroundBytecodeHeap* heap, GroundSize idx);