diff --git a/src/cat/types/ffi.c b/src/cat/types/ffi.c index cd62e08..5165429 100644 --- a/src/cat/types/ffi.c +++ b/src/cat/types/ffi.c @@ -331,6 +331,7 @@ CAT_VALUE_Value *cat_types_ffi_load(const char *path) return cat_types_empty(); } l->base.vtable = &g_library_vtable; + l->base.refcnt = 1; l->handle = h; l->path = strdup(path); return &l->base; @@ -353,6 +354,7 @@ CAT_VALUE_Value *cat_types_ffi_bind(CAT_VALUE_Value *lib, const char *name, if (f == NULL) return cat_types_empty(); f->base.vtable = &g_function_vtable; + f->base.refcnt = 1; f->sym = sym; f->name = strdup(name); f->sig = sig ? strdup(sig) : NULL; @@ -374,6 +376,7 @@ CAT_VALUE_Value *cat_types_ffi_bind_addr(unsigned long long addr, if (f == NULL) return cat_types_empty(); f->base.vtable = &g_function_vtable; + f->base.refcnt = 1; f->sym = (void *)(size_t)addr; f->name = strdup(name ? name : "?"); f->sig = sig ? strdup(sig) : NULL; diff --git a/src/cat/types/file.c b/src/cat/types/file.c index bab794a..b26fe9f 100644 --- a/src/cat/types/file.c +++ b/src/cat/types/file.c @@ -297,6 +297,7 @@ CAT_VALUE_Value *cat_types_file_open(const char *path, const char *mode) return cat_types_empty(); } f->af.base.vtable = &g_file_vtable; + f->af.base.refcnt = 1; f->af.mode = m; f->af.closed = 0; f->af.is_bytes = 0; @@ -312,6 +313,7 @@ CAT_VALUE_Value *cat_types_file_stringio(const char *initial) if (m == NULL) return cat_types_empty(); m->af.base.vtable = &g_memfile_vtable; + m->af.base.refcnt = 1; m->af.mode = CAT_TYPES_FILE_MODE_READ | CAT_TYPES_FILE_MODE_WRITE; m->af.is_bytes = 1; /* in-memory */ m->cap = 128; @@ -342,6 +344,7 @@ CAT_VALUE_Value *cat_types_file_bytesio(void) if (m == NULL) return cat_types_empty(); m->af.base.vtable = &g_memfile_vtable; + m->af.base.refcnt = 1; m->af.mode = CAT_TYPES_FILE_MODE_READ | CAT_TYPES_FILE_MODE_WRITE; m->cap = 128; m->buf = malloc((size_t)m->cap); diff --git a/src/cat/types/list.c b/src/cat/types/list.c index 0b4fcc9..cc6ab64 100644 --- a/src/cat/types/list.c +++ b/src/cat/types/list.c @@ -57,6 +57,8 @@ static int list_set_attribute(CAT_VALUE_Value *self, const char *name, if (idx < 0 || idx >= l->n) return (cat_error_raise(err, "list index %ld out of range", idx), -1); + cat_value_retain(value); + cat_value_release(l->items[idx]); l->items[idx] = value; return 0; } @@ -66,7 +68,10 @@ static int list_set_attribute(CAT_VALUE_Value *self, const char *name, static void list_destroy(CAT_VALUE_Value *self) { CAT_TYPES_LIST_List *l = (CAT_TYPES_LIST_List *)self; + int i; + for (i = 0; i < l->n; i++) + cat_value_release(l->items[i]); free(l->items); free(l); } @@ -115,6 +120,7 @@ int cat_types_list_push(CAT_VALUE_Value *list, CAT_VALUE_Value *v) l->cap = nc; } l->items[l->n++] = v; + cat_value_retain(v); return 0; } diff --git a/src/cat/types/selector.c b/src/cat/types/selector.c index 2e685fc..5c7d709 100644 --- a/src/cat/types/selector.c +++ b/src/cat/types/selector.c @@ -102,8 +102,11 @@ CAT_VALUE_Value *cat_types_selector_lookup(const char *name) return NULL; /* accept both `_dot_` and `.` spellings of a selector */ for (i = 0; i < (int)(sizeof(g_sel) / sizeof(g_sel[0])); i++) { - if (g_sel[i].base.vtable == NULL) + if (g_sel[i].base.vtable == NULL) { g_sel[i].base.vtable = &g_selector_vtable; + /* static singleton: immortal, never freed */ + g_sel[i].base.refcnt = -1; + } } strncpy(key, name, sizeof(key) - 1); key[sizeof(key) - 1] = '\0'; diff --git a/src/cat/types/table.c b/src/cat/types/table.c index cad8d26..0537592 100644 --- a/src/cat/types/table.c +++ b/src/cat/types/table.c @@ -73,12 +73,13 @@ static void table_destroy(CAT_VALUE_Value *self) { CAT_TYPES_TABLE_Table *t = (CAT_TYPES_TABLE_Table *)self; int i; - /* Values are NOT destroyed: the same value object may be referenced - * from several tables (globals, constants, locals), so a recursive - * destroy would double-free. We only release the table's own keys - * and storage. */ - for (i = 0; i < t->npairs; i++) + /* Release the values we own, then our own keys and storage. Values + * are shared, so we drop exactly one reference each; the last holder + * frees them. */ + for (i = 0; i < t->npairs; i++) { + cat_value_release(t->pairs[i].value); free(t->pairs[i].key); + } free(t->pairs); free(t); } @@ -125,6 +126,8 @@ int cat_types_table_set(CAT_VALUE_Value *tbl, const char *key, (void)err; if (idx >= 0) { + cat_value_retain(value); + cat_value_release(t->pairs[idx].value); t->pairs[idx].value = value; return 0; } @@ -138,6 +141,7 @@ int cat_types_table_set(CAT_VALUE_Value *tbl, const char *key, } t->pairs[t->npairs].key = strdup(key); t->pairs[t->npairs].value = value; + cat_value_retain(value); t->npairs++; return 0; } diff --git a/src/cat/vm.c b/src/cat/vm.c index 2664cb5..64212fe 100644 --- a/src/cat/vm.c +++ b/src/cat/vm.c @@ -587,6 +587,9 @@ int cat_vm_run(CAT_VM_State *vm, CAT_BYTECODE_Function *fn, CAT_VALUE_Value **ou memset(&cl, 0, sizeof(cl)); cl.base.vtable = cat_types_function_vtable(); cl.proto = fn; + /* This closure lives on the C stack: mark it immortal so a stray + * release can never try to free it. */ + cat_value_make_immortal(&cl.base); return cat_vm_exec_closure(vm, &cl, NULL, 0, out); }