Lua Garbage Collection

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Lua Garbage Collection

Marco Catunda
Hello,

	Please someone could explain me how garbage collection work in lua.

	Is this code correct?

------------
function gc_fallback( o )
    print( "This is a garbage!" )
end

test = {}
tag = newtag()
settag( test, tag )
settagmethod( tag, "gc", gc_fallback() )
test.message = "Hello!!"
print( test.message )
test = nil
-----------

	I have had this output:
Hello!!

	but, I hoped this output message:
Hello!!
This is a garbage!



--- Marco Catunda
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Lua Garbage Collection

Luiz Henrique de Figueiredo
>From [hidden email] Fri Nov 21 00:32:54 1997

>	Please someone could explain me how garbage collection work in lua.

garbage is collected only after several objetcs have been created.
garbage collection is not triggered by setting a variable to nil.
(otherwise, it could be too slow.)

>	Is this code correct?

the code is correct but doesn't do what you expect. ;-)
if you really want to see the message, call collectgarbage explicitly.
--lhf