lua-users home
lua-l archive

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

Gaspard Bucher wrote:
> From my understanding, when "foobar" or "thread" goes out of scope,
> the whole thing should be garbage collected. What is happening is that
> only the
> userdata inside foobar is collected but foobar is still a table. This
> table does not have any special mode.

I seems that the reason is the weak table. See the thread "Weak tables
and userdata finalization" by Mark Hamburg (2 Sep 2011). 

When the last reference on 'foobar' is gone, it doesn't mean that the
whole island "thread->coroutine->foobar->udata" gets collected
immediately. Finalizer for 'udata' can be called earlier than the thread
is removed from the weak table. As a consequence, the scheduler can
resume the thread which uses an already finalized userdata.

As a solution to the problem, you can manually remove the thread from
the weak table in the foobar's finalizer (if foobar is a table and
you're using Lua 5.1, then you need to place the finalizer somewhere