lua-users home
lua-l archive

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


On Fri, Oct 30, 2009 at 1:43 PM, Kenk <kenk@heroesent.com> wrote:
> Hi there,
>
>   I'm just looking for a 'best-practice' scenario here... Say I've got user
> data all setup and Lua's happily using it. If for whatever reason that user
> data becomes deleted in C code (for example, a window closing, or a routine
> called that deletes an element in the collection that Lua happens to have a
> reference to somewhere else in script).... What's the best way of setting
> this userdata to nil in C and then telling Lua it's no longer around? In
> script I'd set the variable to nil... Not sure how I'd go about that from C.
>
> Thanks!
>
>
>

There is no easy way to force all instances of a particular userdata
to nil. What I would suggest instead is to change the metatable of the
"deleted" userdata to one that throws an error in its __index
metamethod, etc.  Alternatively, you could set the pointer held by the
userdata to NULL, and check that the pointer is not NULL before you do
anything with it (this is what Lua's "file" objects do).

-Duncan