lua-users home
lua-l archive

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


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.