lua-users home
lua-l archive

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

Hello Andrew:

The correct solution is to wrap the library-allocated data in a userdatum whose __gc metamethod frees the data. This way, the data is under the control of the Lua garbage collector. Then you no longer have to explicitly free it at the end of your C function; just make sure the userdatum stays on the Lua stack. And you'll know that the data will always eventually be freed when it's no longer needed, even if a Lua error occurred. For more information, see the chapter on managing resources in the book _Programming in Lua_, the first edition of which is available at no charge online.

Matt Campbell
Lead Programmer
Serotek Corporation
"The Accessibility Anywhere People"