Comment #0 by andrej.mitrovich — 2011-11-27T15:21:59Z
import std.stdio;
import std.file;
import std.process;
void test()
{
auto file = File("test.txt", "r");
foreach (line; file.byLine) // remove and bug goes away
{
}
}
void main()
{
system("echo blabla > test.txt"); // create file externally
test();
std.file.remove("test.txt");
}
std.file.FileException@std\file.d(549): test.txt: The process cannot access the file because it is being used by another process.
Btw this has nothing to do with that system call, you can remove it if you have a test.txt file.
As a workaround I can use scope(exit){ file.close(); }
Comment #1 by mike — 2012-04-08T08:10:35Z
byChunk is also affected by this bug, it's caused by File.detatch which is called by the byLine and byChunk ranges when they reach the end of the file.
But detach currently doesn't decrement the ref count while invalidating a copy of the File.
https://github.com/D-Programming-Language/phobos/pull/530
Comment #2 by ellery-newcomer — 2012-04-08T15:47:39Z
ha. I totally beat you to this bug. but unittests are good.
Note your patch will also fix issue 7831
Comment #3 by lovelydear — 2012-04-23T04:04:53Z
No need to say, you can as a workaround call file.close() after the loop
Comment #4 by github-bugzilla — 2012-05-20T15:34:22Z