lua-users home
lua-l archive

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


OK, Here's what I have come up with (but it doesn't work):

When the script starts, the app executes this new function:

void SetupCDataGC( struct lua_State* L )    // setup garbage collection
{
   luaL_newmetatable( L, "CData_type" ); // create metatable for CData
   lua_pushstring( L, "__gc" );
   lua_pushcfunction( L, CDataDelete );
   lua_settable( L, -3 );
}

The new userdata delete function is declared like this:

int CDataDelete( struct lua_State* L )
{
   // deletion stuff
   return 0;
}

When the lua script creates the CData table (a lua "class"), it calls a CFunction to create the C++ object (existing code). Inside that function, I added the following code:

   CData** ptr = (CData**)lua_newuserdata( L, sizeof(CData*) );
   *ptr = pData;
   luaL_getmetatable( L, "CData_type" );
   lua_setmetatable( L, -2 );

lua_pushlightuserdata( L, pData ); // return the C++ object's pointer to the script
   return 1;


The script executes as usual but the CDataDelete function is not called. So I must still be missing a link in the process.

Michael