lua-users home
lua-l archive

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


On Tue, Oct 20, 2009 at 5:02 PM, Sam Roberts <vieuxtech@gmail.com> wrote:
> On Tue, Oct 20, 2009 at 8:53 AM, Sam Roberts <vieuxtech@gmail.com> wrote:
>> On Tue, Oct 20, 2009 at 8:04 AM, Gary Bringhurst <garyb@strata.com> wrote:
>>> Where can I find a definitive discussion of weak keyed tables and the
>>> behavior of primitive data types as those weak keys?  My tests say that
>>> unique string keys are never collected, and while I can see a justification
>>> for that I'd really like to know the relationship of primitive types such as
>>> strings to the garbage collector. Do we have to use tables or user data as
>>> keys in weak keyed tables in order for them to be collected?
>
> Sorry, it is too early in the morning, I didn't see the "weak".
>
> If that was true, it would surprise me, I would expect a weak-keyed
> table to have a pair cleared from it, both key and value, when the
> value no longer has a reference outside the table.

In my experience: numbers, strings, and boolean values aren't eligible
for collection in this manner:

Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> t = {
>> number = 4,
>> boolean = true,
>> string = "foo",
>> table = {},
>> func = function() end,
>> thread = coroutine.create(function() end),
>> lightud = newproxy(),
>> }
> setmetatable(t, {__mode = "v"})
> collectgarbage("collect")
> collectgarbage("collect")
> for k,v in pairs(t) do print(k, v) end
number	4
string	foo
boolean	true

I've never looked into the implementation reasons for this, but I
suspect there's a fairly good reason =).

- Jim