lua-users home
lua-l archive

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


You can also change your api. Instead of returning an object that you need to close, you run a callback and pass in the object. When the callback returns, close the object. Some notes:

* when closing the object, set a flag so you know that the object is closed and throw an error from methods to avoid dereferencing the closed object.

* try to avoid calling the callback from c code otherwise it won't play nice with coroutines.

On Oct 13, 2015 1:32 AM, "Javier Guerra Giraldez" <javier@guerrag.com> wrote:
On Tue, Oct 13, 2015 at 3:20 AM, Thierry FOURNIER <thi@thierry.1s.fr> wrote:
> I called functions executed in a coroutine. Maybe the developper copy
> the object containig the FD in the global env, or maybe not. It cant
> know this information, so I can't close himself the fd. Only the gc and
> the refcount system (I guess) know the information.


there's no refcount system in Lua.

but you can implement one: on your object initialization you can
register all 'open' objets in a global table, and another table where
you keep those that should stay open.  periodically (or at the pcall()
exit)  close any object that is still in the 'all objects' table and
not in the 'currently needed' table.

--
Javier