Richard Hundt wrote: > So are you saying that if - using your example - I do: > > local x = baz() > > that x can get collected out from under me? I don't think that's right. > I've always relied on cdata being collected when they're not in any GC > roots. Your understanding is correct. Lua stack slots are GC anchors and prevent collection of an object stored to them. OTOH here's a concise example that demonstrates what NOT to do: -- Run this again with the next line enabled and it won't complain. -- collectgarbage("stop") local ffi = require("ffi") local a = ffi.new("int *[?]", 10000) for i=0,9999 do local p = ffi.new("int[10]") -- THIS IS WRONG! -- An array element (or struct field or pointer) does NOT keep -- the cdata object returned by ffi.new alive. The contents of -- cdata objects are not traversed by the GC and cannot serve as -- GC anchors. The stored cdata object will eventually be garbage -- collected and the memory will likely be reused soon. a[i] = p for j=0,i-1 do assert(a[j] ~= p, "found reused memory block") end end --Mike