Re: Random crashes with ABC enabled

  • From: Erik Lindroos <gliptic@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 27 Mar 2014 20:40:22 +0100

The LuaJIT garbage collector doesn't traverse struct members to find
objects because it doesn't know whether a pointer points to an
FFI-allocated object or not. You need to keep the TIntArrayNative alive by
keeping a reference to it in a GC-reachable location (or not allocate it
with plain FFI).


On 27 March 2014 20:19, Denis Golovan <denis.golovan@xxxxxxxxx> wrote:

> Hi Demetri
>
> Your 3d example is a bit scary.
> I use the following code for my native array.
> And I've never made any "anchoring".
>
> I was under impression that by just calling TIntArrayNative.new and
> let's say assigning the result to some global variable, I would not
> have troubles with garbage collection.
>
> Could you comment?
>
> =======================
> ffi.cdef[[
>       #pragma pack(push, 1)
>       typedef struct
>       {
>      NativeInt arr[?];
>       }
>       TIntArrayNative;
>
>       typedef struct
>       {
>      TIntArrayNative *ptr;
>       }
>       PIntArrayNative;
>       #pragma pack(pop)
> ]]
>
> local TIntArrayNative_mt = {
> ...
>    __gc =
>       function(x)
>         ffi.C.TIntArrayNative_gc(x.ptr)
>       end,
> ...
> }
>
> local TIntArrayNative_ctor = ffi.metatype("PIntArrayNative",
> TIntArrayNative_mt)
>
> function TIntArrayFromPtr(p)
>    return TIntArrayNative_ctor(p)
> end
>
> TIntArrayNative = {}
> function TIntArrayNative.new(n)
>       return TIntArrayFromPtr( ffi.C.TIntArrayNative_new(n) )
>  end
> ========================
>
> BR,
> Denis
>
>

Other related posts: