[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Is breaking out of io.lines(file) safe?
- From: Matthias Kluwe <mkluwe@...>
- Date: Sun, 20 Mar 2011 18:41:28 +0100
2011/3/19 Roberto Ierusalimschy <email@example.com>:
>> 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 (and doing business in C++
I tend not to close files opened in function scope, leaving the work
to the destructor).
Look at the following simple example:
local function f( ... )
local of = io.open( 'x.txt', 'w' )
of:write( ... )
for i = 1, 100 do
f( i, '\n' )
This does not yield a file containing '100' on my system...