[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Binding C++ objects using Lua tables, light userdata vs. __gc?
- From: Carsten Fuchs <CarstenFuchs@...>
- Date: Tue, 01 Apr 2008 19:34:17 +0200
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