lua-users home
lua-l archive

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


Graham Wakefield wrote:

? You mean, in __gc set the userdata environment to nil, and then in the type-check test if the environment is nil, for example? Or likewise for a field in the environment? Probably null-checking the userdata value is easier.

I meant for a field in the environment table... Assuming the table is unique for each userdata. I think you are right, though; I have not seen a case where this would be better than either of the other two methods.


Another question: is the userdata's environment garbage collected when __gc is called, or when the userdata itself is garbage collected?


The environment table can be collected like any other table: when there are no remaining references to it. Collecting a userdata merely removes the reference to its environment table.

--------

In most of my libraries, I have close metamethods that are exactly synonymous for __gc. That is, they call the same C function. This is so that I can clean up resources whenever I want, but if I ever forget, the garbage collector is there to cover me. I use 'close' because it is nicer to write that '__gc'.

Mike