I’m having some trouble getting finalizers to run, it seems that in some cases finalizers are only run atexit
, and not when the object seems unreachable and I’ve called gc()
a couple of times.
For example, the following snippet works as expected:
type Foo
field
function Foo()
obj = new(1)
finalizer(obj, finalize)
return obj
end
end
finalized = false
function finalize(obj::Foo)
global finalized
finalized = true
msg = "Finalizing...\n"
ccall(:write, Cssize_t, (Cint, Cstring, Csize_t),
1, msg, length(msg))
end
let
foo = Foo()
end
println("Forcing GC")
for i in 1:10
gc()
end
@assert finalized
However, if I change Foo
not to have any fields, the finalizer isn’t run before the program exits:
type Foo
function Foo()
obj = new()
finalizer(obj, finalize)
return obj
end
end
...
ERROR: LoadError: AssertionError: finalized
Finalizing...
Another one, again starting from the top example from this post (ie. with field
) but this time throwing a MethodError
refering to an instance of foo
:
let
foo = Foo()
@test_throws MethodError sin(foo)
end
...
ERROR: LoadError: AssertionError: finalized
Finalizing...
Are these genuine bugs, or am I expecting something from the gc which it can’t/doesn’t guarantee?
cc @yuyichao