不要缓存 FFI 引入的函数。
引用自 http://luajit.org/ext_ffi_semantics.html
> it's generally
not safe to use function
cdata objects obtained from a library if the namespace object may be
unreferenced.
> It's not
useful and actually counter-productive to explicitly cache these
function objects, e.g.
local strlen = ffi.C.strlen. OTOH it
is useful to cache the namespace itself, e.g.
local C =
ffi.C.
在 2017年8月3日星期四 UTC+8下午4:20:17,lili...@gmail.com写道:
我有一个动态库,提供了add接口如下
int add(int a, int b)
{
return a + b;
}
编译成libtest.so后,在init_worker_by*阶段中加载使用如下,将test.add接口保存到局部变量add中,在定时器中使用add就会coredump。
但是直接使用注释的用法test.add(1, 2)又运行正常。
local ffi = require("ffi")
ffi.cdef[[
int add(int a, int b);
]]
local test = ffi.load("./libtest.so")
local add = test.add
local flush_cache
flush_cache = function(premature)
if not premature then
--test.add(1, 2)
add(1, 2)
local ok, err = new_timer(1, flush_cache)
... ...
end
end
local ok, err = new_timer(1, flush_cache)
... ...
不使用ffi调用*so库,require加载纯lua模块,两种用法都是OK,大神们帮忙把把脉。