lua-users home
lua-l archive

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


AB> As I said I didn't make any assumptions on the order in my 
> code but was wondering if garbage collection has any other 
> rules similar to that on the order of user data collection. 
> Your response clearly indicates that no other rules can be 
> assumed and/or used for making code dependant on them. The 
> thing I wanted to do is this (still want but not as much). 
> Given a co-routine I would like to associate a user data 
> object with it that would get collected before the co-routine 
> itself does. In other words, given a co-routine with no 
> outstanding references to it I would like for some designated 
> user data get garbage collected/finalized before the 
> co-routine itself does. 


Could you use a mechanism like this?

function thingUpdate(object)
  while true do
    if not updateObject(object) then
      object:delete()
      object = nil
      return
    end
    coroutine.yield()
  end
end

....

obj = createObject("type")
coroutine.create(thingUpdate, obj)

We are not using GC to delete user data, we delete it explicitly. Lua
will delete the user data object which wraps the pointer to your
"object", and the coroutine, in its own time in no specific order. You
can't expect Lua to do your user object management in specific ways
using GC as pointed out in other posts. You'll either have to do it
explicitly or put in some sort of object management layer. This should
work for light or full userdata.

I'm assuming you have some sort of destructor code in the object which
must be called before the coroutine ends (e.g. to detach itself from
some lists?). It sounds like you gave the GC metamethod wired up to call
this. I think Ricis posts discusses the pros and cons of this approach.
If this is the case it should probably be removed and an explicit
delete() called instead.

It would be nice to think that data we give to Lua to manage will be
collected in certain ways but this can quickly become application
specific. I appreciate the order of user data deletion is ordered but
when things are mixed you lose this property. Its best to think of
garbage collection as things disappearing off the radar, rather than
forming an orderly queue to get deleted. I have probably missed
something as I don't really understand why you require 2 collection
cycles to delete these two objects.

/Nick