[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Running the GC to clear resource holding object on failure
- From: Tim Hill <drtimhill@...>
- Date: Sat, 2 Aug 2014 09:44:32 -0700
On Aug 2, 2014, at 4:38 AM, Marc Balmer <marc@msys.ch> wrote:
> Hi
>
>
> For now, as a first solution, we call lua_gc(LUA_GCCOLLECT, ...) after lua_pcall(), but is this an expensive operation? Is there a better way to handle such objects that hold resources? Should we only call lua_gc() if lua_pcall() returns an error, or always?
>
> How do others that integrate Lua into C programs deal with such a situation?
>
> Is there a way to mark an object to be immedtiately garbage collected when it is no longer used?
>
Couple of suggestions (both of which we use in out design, which faces similar issues)…
— Use a timeout to trigger a GC in the Lua state if you haven’t called it for a certain period. This maximizes the time before the object is closed, but minimizes unnecessary GC cycles. Also, set a flag once you have done the GC so you only do it once.
— In your APIs that create handles, set a flag to indicate one or more has been created, and only do the GC (resetting the flag) if that flag is set when you exit the Lua pcall (indicating at least one open operation has occurred).
These are both statistical of course, they will occasionally run the GC unnecessarily. You can’t really count the open/close operations to see if they balance, as (i’m assuming) the Lua code may keep the object open between pcalls.
—Tim