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

@@ -529,7 +529,7 @@ struct _Ground {
struct GroundExecutionResult (*execute) (GroundBytecodeInstruction* instruction, GroundBytecodeHeap* heap); struct GroundExecutionResult (*execute) (GroundBytecodeInstruction* instruction, GroundBytecodeHeap* heap);
} Instruction; } Instruction;
struct { struct {
void (*set) (GroundBytecodeHeap* heap, GroundSize idx, GroundValue value); void (*set) (GroundBytecodeHeap* heap, GroundSize idx, GroundBytecodeValue value);
GroundValue* (*get) (GroundBytecodeHeap* heap, GroundSize idx); GroundValue* (*get) (GroundBytecodeHeap* heap, GroundSize idx);
} Heap; } Heap;
} Bytecode; } Bytecode;

View File

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

View File

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

View File

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

View File

@@ -109,7 +109,7 @@ struct GroundExecutionResult _GroundBytecodeInstructionExecute(GroundBytecodeIns
void _GroundBytecodeSave(GroundBytecode* bytecode, const char* path); void _GroundBytecodeSave(GroundBytecode* bytecode, const char* path);
GroundBytecode _GroundBytecodeLoad(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); GroundValue* _GroundBytecodeHeapGet(GroundBytecodeHeap* heap, GroundSize idx);