lua-users home
lua-l archive

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



On Mar 30, 2007, at 9:35 AM, Michael Broughton wrote:

One way you can prevent the __gc metamethod from being called is to create an __index metamethod that filters out private indices. Of course, that would not prevent any C code from calling your finalizer. Overall, I think this solution is too complicated and it will slow down your whole module.

I agree.


I think it is best to just keep track of the state of your userdata and any associated resources. For example: if your userdata is a struct, add a field to mark when it is finalized. If your userdata is a boxed pointer, make sure you nullify the pointer when you are done with it.

Yes, I've done this now... but still I think __gc should be private by default.

You could also use each userdata's environment table to do something similar.

? 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.

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