Closed HertzDevil closed 1 week ago
Yeah I had to wrap my head a couple of times to understand what this is about.
Until I realized that x
is still alive on the stack, so the Bar
instance doesn't get finalized.
When allocating through ReferenceStorage
via malloc_atomic
, the garbage collector doesn't know that there is still a reference to the Foo
instance and thus collects the memory (which calls the finalizer).
So the issue is that Pointer(ReferenceStorage(Bar)).malloc(1)
resolves to GC.malloc_atomic
despite Bar
having inner pointers.
Nothing is printed after running the snippet below, because
Bar
contains an inner pointer, and the compiler will automatically useGC.malloc
:However,
finalize
is printed if we replaceBar.new
with.unsafe_construct
(i.e..pre_initialize
+#initialize
):This is only possible if
Pointer(ReferenceStorage(Bar))
usesGC.malloc_atomic
behind the scenes; indeed, allocating the memory withGC.malloc_atomic(instance_sizeof(Bar))
would give the same result. I don't think this is ever intended, since this blocks some use cases ofReferenceStorage
where multiple objects are allocated at once (more on this later).