Re: 2.1 crash using module with ffi

  • From: Geoff Leyland <geoff_leyland@xxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Fri, 24 Jan 2014 18:45:29 +1300

On 24/01/2014, at 6:37 pm, William Adams <william_a_adams@xxxxxxx> wrote:

> It's more like this situation:
>  
> local buff = ffi.new("char[1024]");
> local contentType = ffi.new("int[1]");
> CFunction(buff, bufflen, contentType)
>  
> -- return the correct struct based on contentType
> if contentType[0] == BITMAP24 then
>   return ffi.cast("BITMAP24 *", buff);
> elseif contentType[0] == BITMAP32 then
>   return ffi.cast("BITMAP32 *", buff);
> end

Ouch.  But could you use malloc?

local buff = ffi.C.malloc(1024);
local contentType = ffi.new("int[1]");
CFunction(buff, bufflen, contentType)
 
-- return the correct struct based on contentType
if contentType[0] == BITMAP24 then
  return ffi.gc(ffi.cast("BITMAP24 *", buff), ffi.C.free);
elseif contentType[0] == BITMAP32 then
  return ffi.gc(ffi.cast("BITMAP32 *", buff), ffi.C.free);
end


Other related posts: