more gc gotchas

  • From: William Adams <william_a_adams@xxxxxxx>
  • To: <luajit@xxxxxxxxxxxxx>
  • Date: Mon, 28 May 2012 13:53:13 +0000

LuaJIT offers some patterns that you don't see in regular Lua.  As discussed 
here previously, you have to be very careful with ffi.cast.  I had code that 
looked similar to the following:

 

local alltext = file:read("*all")

local bytes = ffi.cast("const uint8_t *", alltext)
local size = string.len(alltext)

local rowsize = somenumber

 

callfunc(bytes, size, rowsize)

 

function callfunc(bytes, size, rowsize)

  local bytearray = ffi.new("uint8_t["..size.."]");

  if bytesalign(bytes, size, 4) then

    ffi.copy(bytearray, bytes, size)

  else

    -- not aligned on 4 byte boundary, so

    -- copy one row at a time

    for row=1,height do

      ffi.copy(dst, src, rowsize);

    end

  end

end


 

It's not a complete example, but it serves for this explanation.  In the 
callfunc(), when I was copying memory, one big chunk at a time, there was no 
problem.  I'm assuming because the GC does not kick in any time before that 
operation occurs.  But, when the slower operation of copying a "row" at a time 
was utilized, at some point (mysteriously after 64 iterations through the 
loop), the thing would just crash.  Seeing no obvious problem, I started to 
think about the GC and casts.  In this case, the 'bytes = ffi.cast...' code was 
the culprit.  I changed the code such that the cast now happens withinn 
callfunc() instead, and all is right with the world.

 

local alltext = file:read("*all")

local rowsize = somenumber


callfunc(alltext, rowsize)

 

function callfunc(str, rowsize)

  local bytes = ffi.cast("const uint8_t *", str)

  local size = string.len(str)

 

...

 

This way, the "alltext" will stick around at least as long as the callfunc() 
function is being run.  The cast within the function will also stick around 
long enough, and the underlying buffer won't be cleaned up out from under it as 
was happening before.

 

It's one of those subtle things that a slow brain like mine takes a while to 
catch.  So, I put it here as an example for those who may follow.  Beware the 
gc...

 

-- William

===============================

- Shaping clay is easier than digging it out of the ground.


http://williamaadams.wordpress.com
http://www.thingiverse.com/WilliamAAdams
https://github.com/Wiladams                                       

Other related posts: