lua-users home
lua-l archive

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


On Fri, Feb 22, 2008 at 11:41 AM, Shmuel Zeigerman <shmuz@actcom.co.il> wrote:
> It isn't wrong, but the "generic for" just terminates the loop on
> breaks, so the file remains open until the garbage collector will close
> it. The following example should work (untested):
>
>
>  > for k=1,1000 do
>  >     for line in io.lines() do
>  >         break
>  >     end
>  >     collectgarbage"collect"
>  > end
>
> Wouldn't it be possible to enhance the "generic for" by adding an extra
> "break" call to the iterator (with a nil or false argument), to let it
> do the cleanup when needed? That may break the existing applications
> though (no pun intended).

I would think the easier fix would simply be for io calls which fail
to allocate a file handle to collectgarbage and try again.