Make sure that destructors are called when objects are garbage collected

This commit is contained in:
2025-09-07 15:51:36 -07:00
parent 14fdfbce85
commit b40fcea2c2
2 changed files with 69 additions and 3 deletions

View File

@ -114,6 +114,9 @@ int main(int argc, const char **argv) {
refcount_context_ref(c, a);
refcount_context_unref(c, a);
assert(refcount_context_num_refs(c, a) == 1);
int key;
A *a_with_destructor = a;
assert(refcount_context_add_destructor(c, a, &key, reref_destructor, c));
a = NULL;
for (char i = 'a'; i <= 'z'; ++i) {
@ -124,6 +127,9 @@ int main(int argc, const char **argv) {
assert(refcount_context_num_refs(c, a) == 0);
refcount_context_unref(c, a);
assert(refcount_context_garbage_collect(c) == 0);
assert(refcount_context_num_refs(c, a_with_destructor) == 1);
assert(refcount_context_remove_destructor(c, a_with_destructor, &key));
assert(refcount_context_garbage_collect(c) == 26);
a = make_a(c, 0, "a");
@ -162,7 +168,6 @@ int main(int argc, const char **argv) {
a = make_a(c, 10, "test destructor");
assert(refcount_context_num_refs(c, a) == 0);
int key;
assert(refcount_context_add_destructor(c, a, &key, reref_destructor, c));
assert(refcount_context_num_refs(c, a) == 0);
assert(!refcount_context_unref(c, a));