我正试图找到在Julia中使用终结器的正确方法
请参阅Julia文档:
终结者(x,函数)
当没有程序可访问的x引用时,注册要调用的函数f(x).如果x是位类型,则此函数的行为是不可预测的.
首先,我使用TestModule.jl生成了一个TestModule标准包
#in TestModule.jl
module TestModule
end
finalizer(TestModule,(t)->println("fin"))
Run Code Online (Sandbox Code Playgroud)
还有一个runtest.jl
#in runtest.jl
using Base.Test
using TestModule
Run Code Online (Sandbox Code Playgroud)
然后我尝试测试Package但是在测试通过时我收到了ERROR:
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
Run Code Online (Sandbox Code Playgroud)
之后我安排了另一个测试案例
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
Run Code Online (Sandbox Code Playgroud)
根据以上测试用例,我有一些问题
finalize方法来TestModule生成ERROR?finalize在清除引用时没有调用方法添加finalize模块方法的正确方法是什么
(OS = Ubuntu,Julia Version = 0.4.0)
编辑
正如@Maciek所提到的那样,gc()之后workspace()也要求帮助.
谢谢
恕我直言,workspace不接受任何囚犯,此外,它finalizer仅适用于用户定义和复合类型。
我已经进行了一些测试。看看我的结果:
julia> type Foo
x
Foo(x) = begin obj = new(x); finalizer(obj,(o) -> println("The end.")); return obj end
end
julia> Foo(1)
julia> workspace()
julia> gc()
Module the end.error in running finalizer: ErrorException("task switch not allowed from inside gc finalizer")
The end.error in running finalizer: ErrorException("task switch not allowed from inside gc finalizer")
Run Code Online (Sandbox Code Playgroud)
在模块范围内定义对象的另一个测试:
julia> module FinMod
type T
x::Int
end
finalizer(T(1), (t) -> println("Module the end."))
end
FinMod
julia> FinMod
FinMod
julia> workspace()
julia> gc()
Module the end.error in running finalizer: ErrorException("task switch not allowed from inside gc finalizer")
Run Code Online (Sandbox Code Playgroud)
那么函数(一等对象)呢?
julia> function foo() println("I'm foo") end
foo (generic function with 1 method)
julia> finalizer(foo, (f) -> println("foo function is dead now."))
julia> foo
foo (generic function with 1 method)
julia> workspace()
julia> foo
ERROR: UndefVarError: foo not defined
julia> gc()
julia> #nothing happened
Run Code Online (Sandbox Code Playgroud)
所以,总结一下:在我看来,workspace不调用finalize. 该finalizer函数仅适用于用户定义类型和复合类型。它不适用于Module或Function。
更新:我记得将workspace以前的模块重写Main为LastMain. 因此,即使我们的模块无法从Main它访问,它仍然在范围内存在LastMain(对于我上面使用的函数同样有效)。