diff --git a/std/mmfile.d b/std/mmfile.d index 589e3708fcf..c06858cb142 100644 --- a/std/mmfile.d +++ b/std/mmfile.d @@ -78,6 +78,8 @@ class MmFile version(linux) this(File file, Mode mode = Mode.read, ulong size = 0, void* address = null, size_t window = 0) { + // Save a copy of the File to make sure the fd stays open. + this.file = file; this(file.fileno, mode, size, address, window); } @@ -234,13 +236,25 @@ class MmFile } else hFile = INVALID_HANDLE_VALUE; - scope(failure) if (hFile != INVALID_HANDLE_VALUE) CloseHandle(hFile); + + scope(failure) + { + if (hFile != INVALID_HANDLE_VALUE) + { + CloseHandle(hFile); + hFile = INVALID_HANDLE_VALUE; + } + } int hi = cast(int)(size>>32); hFileMap = CreateFileMappingW(hFile, null, flProtect, hi, cast(uint)size, null); wenforce(hFileMap, "CreateFileMapping"); - scope(failure) CloseHandle(hFileMap); + scope(failure) + { + CloseHandle(hFileMap); + hFileMap = null; + } if (size == 0 && filename != null) { @@ -312,6 +326,7 @@ class MmFile { //printf("\tfstat error, errno = %d\n", errno); .close(fd); + fd = -1; errnoEnforce(false, "Could not stat file "~filename); } @@ -335,10 +350,14 @@ class MmFile size_t initial_map = (window && 2*window