Been taking a stab at LuaJIT FFI and have run into a curious problem. I have the following code:
local ffi = require("ffi")
ffi.cdef [[
int decode(char* encodedToken, char** payload);
]]
local lib = ffi.load("lib.so")
local function decode(encoded_token)
local c_encoded_token = ffi.new("char[?]", #encoded_token, encoded_token)
local c_token = ffi.new("char*[1]")
lib.decode(c_encoded_token, c_token)
local resonse = ffi.string(c_token[0])
return response
end
Basically, the shared object has a method to decode an incoming token, and writes the output to char** payload. The weird part is, this code works when I run a single iteration of the decode function, but when run in a loop, causes Segmentation fault.
This leads me to believe there is a memory leak with my ffi.new("char*[1]") allocation. I even tried wrapping it in a ffi.gc call, which didn't help either:
local c_token = ffi.gc(ffi.new("char*[1]"), ffi.free)
-- do stuff
c_token = nil
This is the first time I'm trying out FFI, so any pointers appreciated!