lua-users home
lua-l archive

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


You are right. As it is described, it does not work properly.
The fix proposed by you would solve the problem.

Thanks for pointing this out.

-- Waldemar

On Apr 1, 2008, at 2:34 PM, Carsten Fuchs wrote:

Dear list,

in the Game Programming Gems 6, chapter 4.2 about binding C/C++ objects to Lua, the chapter presents "Binding Host Objects to Lua Tables" as the last binding method.

It essentially says that when a new C/C++ object is to be made available in Lua,
	a) a new Lua table is created,
b) The "_pointer" field within this table is set to a light userdata item with the pointer to the C/C++ object, c) The metatable corresponding to the type of this object is set for this table, d) an entry into the environment table is added (for uniqueness checking).


Well, what I don't understand is why a *light* userdata item is used here, rather than a full userdata, when thinking about the automatic garbage collection of the C/C++ object.

If I understood the text correctly, the metatable from step c) above has a __gc method properly assigned, but this metatable is now assigned to a *table* rather than to full userdata as in the prior section of the book.

Now, the PiL2 says that __gc only works for userdata, not for tables, and that metatables cannot be set for light userdata anyway. Thus - if I understand this correctly - the C/C++ object in this binding method is never collected, because the __gc method is never called.

Therefore, I would kindly like to ask you for your comments: Is my understanding and conclusions correct? Is the problem properly solved by employing full rather than light userdata (and assigning the metatable with the __gc method also to that userdata)?

Thank you very much,
and best regards,
Carsten




--
Ca3D - Engine    http://www.Ca3D-Engine.de
Carsten Fuchs    http://www.Ca3D-Engine.de/c_Carsten.php