lua-users home
lua-l archive

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


Doug,

It is not true that anyone has told me explicitly to return full userdata using lua_newuserdata(). Perhaps you think someone has told me that, but it isn't so. With all due respect, you've been leaving that particular part for me to guess! I had tried returning the userdata pointer from ptr = lua_newmetatable(), like this:

   lua_pushlightuserdata( L, pData );
   lua_pushlightuserdata( L, ptr );
   return 2;

But, obviously, that did not work. So, after you just now told me *explicitly* that I need to return lua_newuserdata(), rather than the value it returns, I simply moved my C++ object pointer up above the call to lua_newuserdata() as shown below.

   // C++ object pointer pData exists at this point
   lua_pushlightuserdata( L, pData );    // my object pointer

   // create a userdata and add it to the userdata metatable for CData
   CData** ptr = (CData**)lua_newuserdata( L, sizeof(CData*) );
   *ptr = pData;
   luaL_getmetatable( L, "CData_type" );
   lua_setmetatable( L, -2 );
   return 2;

After that change, the __gc is not called right away and it does get called correctly at the end of the script. I'll do some testing and see if this change solves the problem. Thanks for mentioning lua_newuserdata() explitly.

BTW, Yes, I know I can get the C++ object pointer from the userdata, so I don;t need both. If this code now gives me what I want, then I'll see about cleaning that up.

Michael

----- Original Message ----- From: "Doug Currie" <doug.currie@gmail.com>
To: "Lua list" <lua@bazar2.conectiva.com.br>
Sent: Sunday, August 16, 2009 12:02 PM
Subject: Re: how does GC work with complex expressions using metamethods?



On Aug 16, 2009, at 2:54 PM, Michael Newberry wrote:

No, I returned both lightuserdata and the metatable containing the GC method.

You did not return (full) userdata. You returned lightuserdata. We are all telling you, over and over again, this won't work.

I also tried returning the pointer to the userdata using lua_pushlightuserdata and that made no difference.

lua_pushlightuserdata does not return (full) userdata.

lua_newuserdata pushes the (full) userdata on the Lua stack. That is what you need to return from your function.

Remember I am returning two values, and I am assigning the second value to a table element back in the lua code. The two return values are stored in a table which I has setup as a class.

That doesn't help since neither is the (full) userdata you need to  store.

e