lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi, I've got the point! Thank you.

>  Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
>
[...]
>  
>    Because Lua has automatic memory management and garbage collection,
>    a |lua_Object| has a limited scope,
>    and is only valid inside the block where it has been created.
>    A C function called from Lua is a block,
>    and its parameters are valid only until its end.
>    It is good programming practice to convert Lua objects to C values
>    as soon as they are available,
>    and never to store |lua_Object|s in C global variables.


This was exactly the problem. I used 

typedef lua_Object oluaObject

and stored lua_Objects in C. Now I am using references to objects
and everything works well:

typedef struct {int ref;} oluaObject;
extern oluaObject oluaNull;


oluaObject oluaCreateRef(lua_Object o)
{
  oluaObject oo;
  lua_pushobject(o);
  oo.ref=lua_ref(1);
  return oo;
}

lua_Object oluaGetObject(oluaObject o)
{
  lua_Object xo;
  if (o.ref==oluaNull.ref) return LUA_NOOBJECT;
  xo=lua_getref(o.ref);
  return xo;
}

...
at the top of module initialization:
oluaNull=oluaCreateRef(lua_createtable());
...

BTW, Norman, This solves the  problem of compiler checking objects
and their references for me, otherwise I wouldn't have been able
to modify the code within one evening...


Best regards

Juergen

Dr. Juergen Fuhrmann                   mailto:fuhrmann@wias-berlin.de
                         Numerical Mathematics & Scientific Computing
    Weierstrass Institute for Applied Analysis and Stochastics Berlin
SOMETHING IS ROTTEN IN THIS AGE OF HOPE heiner mueller hamletmaschine