Bug 7022 – File.byLine doesn't release file handle

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-11-27T15:21:00Z
Last change time
2012-05-31T04:05:28Z
Assigned to
nobody
Creator
andrej.mitrovich
Depends on
7831
Blocks
7659

Comments

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
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/f1566faeafec6715f227cb7d3bbe3af3737de4b3 fix issue 7022 fix issue 7831 detach sets p=null, which makes it kind of hard for the destructor to actually close the file or decrement the reference count. https://github.com/D-Programming-Language/phobos/commit/f41c9c48a7edca92fbc0f4f2bf81a7d99c79fcc2 Merge pull request #527 from mylodon/fix-issues-7831-7022 fix issue 7022
Comment #5 by github-bugzilla — 2012-05-28T14:19:34Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/bff3254afc020df1fd55565930c394993334d509 File.detach should decrement the ref count - Issue 7022 Because detach invalidates a copy of the File struct it should also decrement the ref count. The File destructor doesn't take care of this because by then p is already null. https://github.com/D-Programming-Language/phobos/commit/fa52571d8c7d128393fae5b61f0018d53726425f Merge pull request #530 from MikeWey/patch-1 File.detach should decrement the ref count - Issue 7022