lua-users home
lua-l archive

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


On 20 March 2011 17:41, Matthias Kluwe <mkluwe@gmail.com> wrote:
> Hi!
>
> 2011/3/19 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>>> Yes, the file handle will still be open, at least until the file
>>> object is garbage collected. There is no way for an iterator to detect
>>> a broken loop and run cleanup code, so you will need to make sure the
>>> handle is closed explicitly.
>>
>> Or simply leave it to the garbage collection. (More often than not there
>> is no hurry to close the file.)
>
> Hmm, until now it bit me twice not doing so

I too have been bitten by this a number of times.

> Look at the following simple example:
>
> local function f( ... )
>    local of = io.open( 'x.txt', 'w' )
>    of:write( ... )
> end
>

I think it's especially wise to be careful when dealing with files
opened with "w".

Regards,
Matthew