lua-users home
lua-l archive

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

On 4-Dec-06, at 10:57 AM, Carsten Fuchs wrote:

Dear list,

I was just wondering what the proper way is to "unregister" a function that has been registered with a call to lua_pushcfunction() or luaL_register() before. This situation occurs whenever a "plugin DLL" registers functions to a Lua state of the main program, and wants to withdraw them again when it is unloaded.

Is it sufficient to set the variable that holds the function to nil?
The problem is that if someone makes a copy of the variable with the function, and continues to use the copy to call the function after the DLL has been unloaded, I expect to get a bad crash... Is there anything I can do to prevent this kind of scenario?

One possibility, maybe, is to unload the DLL using a __gc metamethod. If every function defined in the DLL has a reference to a userdata representing the DLL, then you could unload the DLL when that userdata gets collected. You might be able to attach the reference by providing an appropriate function environment to the functions registered by the DLL.

If the DLLs can use an interface other than luaL_register / lua_pushcfunction (which may not be very convenient), you could add a level of indirection to allow them to withdraw registration.