[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Collected objects still in table?
- From: Philipp Janda <siffiejoe@...>
- Date: Fri, 21 Mar 2014 17:50:20 +0100
Am 21.03.2014 01:54 schröbte Rena:
I've just noticed a strange issue with garbage collection in my Lua code.
The code is quite large, would be difficult to create a test case, but the
1) Create a userdata object
2) Store it as a key in a table in the registry
3) Create more objects
4) Close the Lua state
5) The object created in step 1 is collected and its __gc is called
6) Other objects' __gc look in the table in the registry, and that object
is still there!
How can __gc be called and the object not be removed from the table?
Lua's GC only modifies weak tables. If a non-weak table contains a
reference to a value that is about to be garbage-collected, then the
table must be garbage as well, so there is no need to update the table
because it will soon be collected anyway.
The obvious exception is `__gc` metamethods. If your finalizers rely on
the fact that a table only contains non-finalized objects, then
remove the objects from the table yourself during `__gc`.