Garbage collecting in general

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

Garbage collecting in general

Falko Poiker
Maybe I'm just doing something wrong, or I don't get it.  I'll try to be as
clear as possible.

I have a lua userdata type called "selection" that is a mirror of a
selection class we have in our game.  This lua type has a garbage collection
tag method defined for it that deletes a selection instance in the game (the
one being pointed to by the userdata object).

Our game runs a lua file to read in a series of lua functions (which define
different portions of an AI state machine), then calls these lua functions
as needed.  In these functions local variables are created of the type
selection (globals as well, but we'll ignore that for now).  I was under the
impression that lua, having a garbage collection system, would at some point
gc these variables automatically, and as a result call the gc tag method
which would delete the selection instance in the game.  This does not
happen.  At no point does the gc tag method get called for any of the
selections created in lua, even when the state is closed.  I even took lhf's
advice and forced garbage collection, but even doing this didn't trigger the
gc tag method, nor the deletion of these selection instances.

Why is garbage collection not working for me?  Am I doing something
fundamentally wrong here?

Any help would be appreciated.
Thank you.
Falko

Reply | Threaded
Open this post in threaded view
|

Re: Garbage collecting in general

Roberto Ierusalimschy
> At no point does the gc tag method get called for any of the
> selections created in lua, even when the state is closed.

This seems rather strange. When you close a state, the gc tag methods for
all elements are called.


> Why is garbage collection not working for me?  Am I doing something
> fundamentally wrong here?

It can be just some detail wrong. Does your userdata have the correct tag?
Are you setting the gc tag method correctly? Is it working without errors?
(an error in a gc tag-method handler can have bad consequences...)

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: Garbage collecting in general

Edgar Toernig
In reply to this post by Falko Poiker
Hi,

Falko Poiker wrote:
> 
> I have a lua userdata type called "selection" that is a mirror of a
> selection class we have in our game.  This lua type has a garbage collection
> tag method defined for it that deletes a selection instance in the game (the
> one being pointed to by the userdata object).
>[...] 
> Why is garbage collection not working for me?  Am I doing something
> fundamentally wrong here?

Some things you should check: First, are you using the correct tag in all
places?  Does the userdata get the right one?  Is the gc-method set for
the same?  Then, are you sure you do not create additional references to
your selection userdata?  Example:

	function foo()
		local x = make_selection(...) -- returns the userdata
		y = x
		...
	end

Here after executing foo you still have a reference to your selection
in the global y.  And as long as this reference exists your gc-method
will not be called!  An y=nil/0/""/... is required to remove this ref-
erence.  It's not like in some languages that the destruction of x calls
the destructor (the gc-method).  Destroying the last reference to an
object calls it.  And last, did you create references within the C
program via lua_ref(L, 1)?  Make sure that _each_ lua_ref call has the
appropriate lua_unref (and this can't be in the gc-method!).

Ciao, ET.