lua-users home
lua-l archive

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


Alexey Malyshev wrote:
This code example causes error after ~500 for loop iterations.

for k=1,1000 do
    for line in io.lines() do
        break
    end
end

When iteration cycle breaks, file handles doesn't close.
Does break keyword wrong for iterator's loop?

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).

--
Shmuel