[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: object persistence across do_file calls
- From: "mieow mieow" <mw218@...>
- Date: Wed, 24 Nov 2004 09:30:37 -0800
i'm new to lua. i have this code:
P = { c = 0 }
function P:run (o)
if o == nil then
o = {} setmetatable(o, self) self.__index = self
self.c = 999
end
print(self.c)
return o
end
for i=1,2 do
v = P:run(v)
--collectgarbage()
end
so, the object uses a global (v) to determine is it's been initialized or
not. note on initialization it sets the value of c to 999. the problem is
that if i execute this script twice in a row with 2 do file calls, ie,
lua_dofile(luavm, "P.lua");
lua_dofile(luavm, "P.lua");
then v persists, but c gets reverted to 0. (or null if i remove the c=0
line). it's as if the object does not persist, even though its handle
remains. i thought it has to do with garbage collection. i tried to force
collection in a loop to see if the object gets destroyed, but it does not.
can someone explain what's going on ? i would like the object state to be
kept so that whatever is there in the object has the same values accross
dofile calls.
_________________________________________________________________
On the road to retirement? Check out MSN Life Events for advice on how to
get there! http://lifeevents.msn.com/category.aspx?cid=Retirement