RE: 2.1 crash using module with ffi

  • From: William Adams <william_a_adams@xxxxxxx>
  • To: "luajit@xxxxxxxxxxxxx" <luajit@xxxxxxxxxxxxx>
  • Date: Fri, 24 Jan 2014 06:04:57 +0000

Yah, you're right.  for this particular situation it would work out.
 
Even better is that the BITMAP24 structure actually has a __gc metamethod, so I 
could just let that deal with the deallocation as well.
 
another situation I have is where there is an array of values returned, and I 
need to hand out pointers to those individual records, and hope the array 
sticks around long enough.
 
In this case, again it might be most reasonable to use malloc as you suggest, 
and possibly just use __gc again to deal with the cleanup, although that will 
only deal with when the entire array goes out of scope, and not the individual 
references within the array.  Probably the best to do in these cases is to copy 
the data out into longer lived structures.
 
-- William

===============================
- Shaping clay is easier than digging it out of the ground.
 
> Subject: Re: 2.1 crash using module with ffi
> From: geoff_leyland@xxxxxxxxxxx
> Date: Fri, 24 Jan 2014 18:45:29 +1300
> To: luajit@xxxxxxxxxxxxx
> 
> 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: