cdata upvalue and garbage collection

  • From: Shmuel Zeigerman <shmuz@xxxxxxxxxx>
  • To: LuaJIT list <luajit@xxxxxxxxxxxxx>
  • Date: Mon, 03 Oct 2016 14:51:11 +0300

Hi list,
I have a script that crashes the application from time to time. Its simplified variant is shown below.
My investigation shows that the crash is due to *rsm* cdata value is accessed after being garbage collected.

I thought that being an upvalue to some live function was enough to be protected from GC but this case shows that additionally an upvalue should be accessed (as a Lua value, not as a cdata field) from that function. If I uncomment "local foo = rsm" then there is no more crashes anymore.

The question is: is that normal behavior? Is being an upvalue to a live function not sufficient to avoid being GC'ed?

--------------------------------
local ffi = require'ffi'
local RS = ffi.new("struct RegExpSearch")
local rsm = ffi.new("struct RegExpMatch[1]")
RS.Match = rsm

LoadCustomSortMode {
  Compare = function()
    -- local foo = rsm
    RegExpControl(RS)
  end;
}

--
Shmuel


Other related posts: