[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: table question
- From: "Virgil Smith" <Virgil@...>
- Date: Mon, 26 Jul 2004 10:51:58 -0500
Lua Digest, Vol 67, Issue 1I'm not sure why you would think that removing an
object from one table would cause it to be removed from somewhere else,
perhaps the distinction between variables and values is still giving you
grief in learning Lua. You cannot change an object to nil. All you can do
is assign nil to variables that reference that object. When all such
references are gone the garbage collector can then destroy the object
because it is no longer in use. What you describe would be destroying an
object despite the fact that some piece of code still has access to that
object.
However, while you can not literally destroy this object while something is
referencing it, you can simulate this by removing all of the MEMBERS of the
object. After that any access to the object will cause an error unless they
specifically check their access. Once you've done this it would probably be
helpful to build a metatable that provides helpful error descriptions (via
the __index metamethod). Furthermore, you will have to provide such a
metatable if you wish to catch attempts to SET a member of the object as
opposed to GETing a member of the object (you would do this via the
__newindex metamethod).
There are some differences, however, if your "object" is a full userdata or
function rather than a table...
In the case of a full userdata, you can't nil out all of its members.
Instead, just set its metatable to one that provides errors on accesses.
Before replacing the full userdata's metatable, however, you should check
for a __gc metamethod and call it if present.
In the case of a function based object I think you will just have to support
a message telling the object to destroy itself and the details of handling
this don't leap immediately to mind. Of course if you have no idea what I
mean by a "function based object", then just don't worry about this case at
all. ;-)
-----Original Message-----
From: Michael Cumming [mailto:lua-bounces@bazar2.conectiva.com.br]On
Behalf Of Michael Cumming
Sent: Monday, July 26, 2004 8:53 AM
To: lua@bazar2.conectiva.com.br
Subject: table question
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
<<attachment: winmail.dat>>