Fixes a file handle leak in mono_mmap_open_file() on Windows. See
authorNiklas Therning <niklas@therning.org>
Tue, 16 May 2017 14:46:52 +0000 (16:46 +0200)
committerNiklas Therning <niklas@therning.org>
Tue, 16 May 2017 17:35:11 +0000 (19:35 +0200)
https://bugzilla.xamarin.com/show_bug.cgi?id=56493.

mcs/class/System.Core/Test/System.IO.MemoryMappedFiles/MemoryMappedFileTest.cs
mono/metadata/file-mmap-windows.c

index 9c273b84a596e848680054d9889147050a6b2530..c192c397fa82fda4a06ff5418e4c0aab4e5bfca4 100644 (file)
@@ -451,5 +451,20 @@ namespace MonoTests.System.IO.MemoryMappedFiles {
                        }
                }
 
+               [Test]
+               public void OpenSameFileMultipleTimes ()
+               {
+                       // See bug 56493 - https://bugzilla.xamarin.com/show_bug.cgi?id=56493
+                       for (var iteration = 0; iteration < 5; iteration++) {
+                               using (var mmf = MemoryMappedFile.CreateFromFile(fname, FileMode.Open)) {
+                                       using (var accessor = mmf.CreateViewAccessor(0, 5)) {
+                                               var a = new byte [5];
+                                               accessor.ReadArray (0, a, 0, a.Length);
+                                               var s = new string (Array.ConvertAll (a, b => (char) b));
+                                               Assert.AreEqual ("Hello", s);
+                                       }
+                               }
+                       }
+               }
        }
 }
index 1ecf2dedbe8299f8c78ff61ca1169fde19acfc8b..e6d8cfd9a1edb0cbe737d9d41df80cded04387cf 100644 (file)
@@ -271,6 +271,8 @@ void *mono_mmap_open_file (MonoString *path, int mode, MonoString *mapName, gint
        result = open_handle (hFile, mapName, mode, capacity, access, options, error);
 
 done:
+       if (hFile != INVALID_HANDLE_VALUE)
+               CloseHandle (hFile);
        if (!result && delete_on_error)
                DeleteFileW (w_path);
        if (w_path)