[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Userdata and GC
- From: Ty Matthews <tmatthews3@...>
- Date: Mon, 7 Jul 2003 11:08:12 -0500 (CDT)
I have a few Userdata implementation questions related to the C API, with 5.0. Included at the bottom is some code.
1. Is it safe to assume that Lua will free the memory that is allocated by lua_newuserdata(), if I do nothing more than this?
2. Are there any caveats that I should be aware of when letting Lua free the allocated block, especially in the realm of C++? IE, if I put a class in my struct, will the destructor be called, etc?
3. If lua_newuserdata() returns NULL, what can I assume about the stack? Does anything get pushed onto the stack in the case of a failure?
4. Are there any sample programs out there that detail how to set a GC metamethod so I can try and free my own memory? Is there a general rule for when you should and should not free your own memory?
Sorry for the question spam. I've been using Lua in a program of mine for close to a year now. I just recently moved to 5.0 and started to get into some more advanced topics. This Userdata concept is new to me, and I haven't gotten a grip on it just by reading the manual.
Thanks for any help-
Ty
class CMyClass
{
.... // assume some implementation
};
int MyTest( lua_State *pState )
{
typedef struct
{
int i;
CMyClass MyClass;
} MyStruct, *LPMyStruct;
int iPush = 0;
int iTop = lua_gettop( pState );
LPMyStruct lpMyStruct = NULL;
void *pUserData = lua_newuserdata( pState, sizeof( MyStruct ) );
if( pUserData )
{
iPush++;
lpMyStruct = static_cast< LPMyStruct >( pUserData );
.... // do stuff with the struct members; initialize, etc.
}
return iPush;
}