I am trying to find the right way to use finalizers in Julia
Refer to Julia documentation:
finalizer(x, function)
Register a function f(x) to be called when there are no program-accessible references to x. The behavior of this function is unpredictable if x is of a bits type.
First I genetated a TestModule standard package with a TestModule.jl
#in TestModule.jl
module TestModule
end
finalizer(TestModule,(t)->println("fin"))
and also a runtest.jl
#in runtest.jl
using Base.Test
using TestModule
then I tried to test Package but I received ERROR while the test was passed:
julia> Pkg.test("TestModule")
INFO: Testing TestModule
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
jl_uv_writecb() ERROR: bad file descriptor EBADF
INFO: TestModule tests passed
after that I arranged another test case
julia> workspace() # new workspace
julia> typeof(TestModule) # make sure *there are no program-accessible references to `TestModule`*
ERROR: UndefVarError: TestModule not defined
julia> using TestModule
julia> finalize(TestModule)
fin # finalize method is working
julia> typeof(TestModule)
Module # make sure *there is program-accessible reference to `TestModule`*
julia> workspace() # force clear references
julia> typeof(TestModule) # check that *there are no program-accessible references*
ERROR: UndefVarError: TestModule not defined
According to above test cases I have some questions
Why adding such
finalize
method forTestModule
generates ERROR during test process?Why
finalize
method was not called while I clear referencesWhat is the right way to add
finalize
method for a module(OS=Ubuntu , Julia Version=0.4.0)
EDIT
as @Maciek have mentioned, calling gc()
after workspace()
also, do not help.
thanks