lua-users home
lua-l archive

Re: table question

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


The problem is that you are expecting c to be nil. c is a table that
you are putting an item at index 1 with the value "test" in it.
Setting t[1] to nil makes the item 1 in table t not link to your new
table object o, that is also referenced to by c.
Thats pretty confusing now that I reread it.
To put it in C terms.
you create a new object (o), and assign the pointer c to it.
so c holds the address of o. While creating it, you set one of the
elements of an array (t) also to point at the o address.
By then setting t[1] to null, c still points at it, and it won't be
garbage collected.
You need to:
t[1] = nil
c = nil
Then the table will garbage collect.
Hope that helps.
Ryan
On 2004年7月26日 09:53:11 -0500, Michael Cumming <mike@4831.com> wrote:
> I am using Lua 4 and LuaCom. I would like to maintain a "master" table of all open objects. I have been playing around with some Lua code to see how tables are handled. The code below is a quick and dirty test of creating an object which is returned to the caller and also stored in a master table. I would like to be able to "destroy" the object by iterating the master table. But if I set an the object in the master table to to nil, the table returned to the caller still exists. Is there a way around this?
> 
> t = {} -- master list of open objects
> 
> function newobject ()
> local o
> o = {"test"} -- normally would be "creatobject"
> tinsert (t,o)
> return o
> end
> 
> c = newobject ()
> 
> print (c)
> print (t [1])
> 
> t [1] = nil
> 
> print (c)
> print (t [1])
> 
> OUTPUT
> 
> table: 0157B360
> table: 0157B360
> table: 0157B360
> nil
> 
> Thanks,
> 
> Mike
> 
> 
> 
>

AltStyle によって変換されたページ (->オリジナル) /