fix memory leak that occures when converting a hashmap to a string in debug mode

This commit is contained in:
William Bell
2025-11-11 19:57:24 +00:00
parent 0b594d7882
commit 0a2aa7369f
5 changed files with 17 additions and 16 deletions

View File

@@ -134,7 +134,8 @@ void run_call(ArgonObject *original_object, size_t argc, ArgonObject **argv,
0);
}
if (CStackFrame) {
ArgonObject * registers[MAX_REGISTERS]; // fixed on the stack for speed purposes
ArgonObject ** registers = ar_alloc(object->value.argon_fn->translated.registerCount *
sizeof(ArgonObject *)); // fixed on the stack for speed purposes
StackFrame new_stackFrame = {
{object->value.argon_fn->translated.registerCount,
object->value.argon_fn->translated.registerAssignment,

View File

@@ -39,18 +39,18 @@ static int compare_node_asc(const void *a, const void *b) {
return 0;
}
void hashmap_GC_to_array(struct hashmap_GC *t, struct node_GC ***array,
struct node_GC ** hashmap_GC_to_array(struct hashmap_GC *t,
size_t *array_length) {
size_t array_size = 8;
*array_length = 0;
*array = ar_alloc(array_size * sizeof(struct node_GC*));
struct node_GC ** array = ar_alloc(array_size * sizeof(struct node_GC*));
for (size_t i = 0; i < t->inline_count; i++) {
if (*array_length >= array_size) {
array_size *= 2;
*array = ar_realloc(*array, array_size * sizeof(struct node_GC*));
array = ar_realloc(array, array_size * sizeof(struct node_GC*));
}
(*array)[(*array_length)++] = &t->inline_values[i];
array[(*array_length)++] = &t->inline_values[i];
}
for (size_t i = 0; i < t->size; i++) {
@@ -59,14 +59,15 @@ void hashmap_GC_to_array(struct hashmap_GC *t, struct node_GC ***array,
while (temp) {
if (*array_length >= array_size) {
array_size *= 2;
*array = ar_realloc(*array, array_size * sizeof(struct node_GC*));
array = ar_realloc(array, array_size * sizeof(struct node_GC*));
}
(*array)[(*array_length)++] = temp;
array[(*array_length)++] = temp;
temp = temp->next;
}
}
qsort(*array, *array_length, sizeof(struct node_GC*), compare_node_asc);
qsort(array, *array_length, sizeof(struct node_GC*), compare_node_asc);
return array;
}
void clear_hashmap_GC(struct hashmap_GC *t) {

View File

@@ -33,7 +33,7 @@ struct hashmap_GC *createHashmap_GC();
void clear_hashmap_GC(struct hashmap_GC *t);
void hashmap_GC_to_array(struct hashmap_GC *t, struct node_GC ***array,
struct node_GC ** hashmap_GC_to_array(struct hashmap_GC *t,
size_t *array_length);
int hashCode_GC(struct hashmap_GC *t, uint64_t hash);

View File

@@ -45,16 +45,15 @@ ArgonObject *create_ARGON_DICTIONARY_TYPE___string__(size_t argc,
ArgonObject *object = argv[0];
size_t string_length = 0;
char *string = NULL;
struct node_GC **keys;
size_t keys_length;
hashmap_GC_to_array(object->value.as_hashmap, &keys, &keys_length);
size_t nodes_length;
struct node_GC ** nodes = hashmap_GC_to_array(object->value.as_hashmap, &nodes_length);
char *string_obj = "{";
size_t length = strlen(string_obj);
string = realloc(string, string_length + length);
memcpy(string + string_length, string_obj, length);
string_length += length;
for (size_t i = 0; i < keys_length; i++) {
struct node_GC *node = keys[i];
for (size_t i = 0; i < nodes_length; i++) {
struct node_GC *node = nodes[i];
ArgonObject *key = node->key;
ArgonObject *value = node->val;
@@ -105,7 +104,7 @@ ArgonObject *create_ARGON_DICTIONARY_TYPE___string__(size_t argc,
string_length += length;
}
if (i != keys_length - 1) {
if (i != nodes_length - 1) {
char *string_obj = ", ";
size_t length = strlen(string_obj);
string = realloc(string, string_length + length);

View File

@@ -1,2 +1,2 @@
while (true) do
string(global)
term.log(global)