lua-users home
lua-l archive

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

>    * lua_newuserdata() is not what I need, because I already have
>      allocated memory...
>    * lua_pushlightuserdata() have no sense for me, because I need to
>      know something about stored pointer and realy need use garbage
>      collector..
>    * I have found lua_boxpointer() macro but it has no use for me (just
>      imagine what will happen when you use boxpointer twice for the
>      same data pointer -- it can happen when you use callbacks -- now
>      try to use garbagecollector).  

If you need to be told when a userdata is GC'd then you need to use full
userdata, not light userdata. This means that you need to use lua_newuserdata
or more conveniently lua_boxpointer. Each time you call lua_newuserdata a new
Lua value is created, even if the same C pointer is stored in it. Lua does not
look inside the buffer allocated by lua_newuserdata. If you need uniqueness,
then create a weak table indexed by your C pointers with values equal to the
corresponding userdata and only call lua_newuserdata or lua_boxpointer if the
pointer is not in the table. Note, a WEAK table is needed, otherwise the value
will never be GC'd.

This has been discussed before here. Please search the archives. Also, I recall
that someone recently said they had a library that did all that checking and
maintainance; I don't known whether it's freely available, but it shouldn't
be hard to write.