[bcl] Add a finalizer to MemoryMappedFile.
authorZoltan Varga <vargaz@gmail.com>
Thu, 23 Mar 2017 17:34:57 +0000 (13:34 -0400)
committerMarek Safar <marek.safar@gmail.com>
Fri, 24 Mar 2017 13:35:18 +0000 (14:35 +0100)
mcs/class/System.Core/Microsoft.Win32.SafeHandles/SafeMemoryMappedFileHandle.cs
mcs/class/System.Core/System.IO.MemoryMappedFiles/MemoryMappedFile.cs

index a862bf05db1389c0c338d9b86974f51f9e5c3fce..85b3fd50c14313ffaac6c55fa66b7d3b0c17bc68 100644 (file)
@@ -31,6 +31,7 @@ using System.Runtime.InteropServices;
 using System.Security.AccessControl;
 using System.Security.Permissions;
 using System.Security.Principal;
+using System.IO.MemoryMappedFiles;
 
 namespace Microsoft.Win32.SafeHandles
 {
@@ -42,10 +43,11 @@ namespace Microsoft.Win32.SafeHandles
                        handle = preexistingHandle;
                }
 
-               [MonoTODO]
                protected override bool ReleaseHandle ()
                {
-                       throw new NotImplementedException ();
+                       MemoryMapImpl.CloseMapping (handle);
+                       handle = IntPtr.Zero;
+                       return true;
                }
        }
 }
index 7a3cce164f61e2feffc9b46fcd751d1c75626e91..4adaaf514febde7d264e247301a62100237a3c0c 100644 (file)
@@ -33,7 +33,6 @@ using Microsoft.Win32.SafeHandles;
 using System.Runtime.InteropServices;
 using System.Runtime.CompilerServices;
 
-
 namespace System.IO.MemoryMappedFiles
 {
        internal static class MemoryMapImpl {
@@ -113,7 +112,6 @@ namespace System.IO.MemoryMappedFiles
                }
        }
 
-
        public class MemoryMappedFile : IDisposable {
                // MemoryMappedFileAccess fileAccess;
                // string name;
@@ -127,7 +125,7 @@ namespace System.IO.MemoryMappedFiles
                //
                FileStream stream;
                bool keepOpen;
-               IntPtr handle;
+               SafeMemoryMappedFileHandle handle;
 
                public static MemoryMappedFile CreateFromFile (string path)
                {
@@ -147,7 +145,7 @@ namespace System.IO.MemoryMappedFiles
                        IntPtr handle = MemoryMapImpl.OpenFile (path, mode, null, out capacity, MemoryMappedFileAccess.ReadWrite, MemoryMappedFileOptions.None);
 
                        return new MemoryMappedFile () {
-                               handle = handle,
+                               handle = new SafeMemoryMappedFileHandle (handle, true),
                                // fileAccess = MemoryMappedFileAccess.ReadWrite,
                                // fileCapacity = capacity
                        };
@@ -179,7 +177,7 @@ namespace System.IO.MemoryMappedFiles
                        IntPtr handle = MemoryMapImpl.OpenFile (path, mode, mapName, out capacity, access, MemoryMappedFileOptions.None);
                        
                        return new MemoryMappedFile () {
-                               handle = handle,
+                               handle = new SafeMemoryMappedFileHandle (handle, true),
                                // fileAccess = access,
                                // name = mapName,
                                // fileCapacity = capacity
@@ -202,7 +200,7 @@ namespace System.IO.MemoryMappedFiles
                        MemoryMapImpl.ConfigureHandleInheritability (handle, inheritability);
                                
                        return new MemoryMappedFile () {
-                               handle = handle,
+                               handle = new SafeMemoryMappedFileHandle (handle, true),
                                // fileAccess = access,
                                // name = mapName,
                                // fileCapacity = capacity,
@@ -229,7 +227,7 @@ namespace System.IO.MemoryMappedFiles
                        MemoryMapImpl.ConfigureHandleInheritability (handle, inheritability);
                                
                        return new MemoryMappedFile () {
-                               handle = handle,
+                               handle = new SafeMemoryMappedFileHandle (handle, true),
                                // fileAccess = access,
                                // name = mapName,
                                // fileCapacity = capacity,
@@ -252,7 +250,7 @@ namespace System.IO.MemoryMappedFiles
                        IntPtr handle = MemoryMapImpl.OpenFile (null, mode, mapName, out capacity, access, options);
                        
                        return new MemoryMappedFile () {
-                               handle = handle,
+                               handle = new SafeMemoryMappedFileHandle (handle, true),
                                // fileAccess = access,
                                // name = mapName,
                                // fileCapacity = capacity
@@ -339,7 +337,7 @@ namespace System.IO.MemoryMappedFiles
 
                public MemoryMappedViewStream CreateViewStream (long offset, long size, MemoryMappedFileAccess access)
                {
-                       var view = MemoryMappedView.Create (handle, offset, size, access);
+                       var view = MemoryMappedView.Create (handle.DangerousGetHandle (), offset, size, access);
                        return new MemoryMappedViewStream (view);
                }
 
@@ -355,7 +353,7 @@ namespace System.IO.MemoryMappedFiles
 
                public MemoryMappedViewAccessor CreateViewAccessor (long offset, long size, MemoryMappedFileAccess access)
                {
-                       var view = MemoryMappedView.Create (handle, offset, size, access);
+                       var view = MemoryMappedView.Create (handle.DangerousGetHandle (), offset, size, access);
                        return new MemoryMappedViewAccessor (view);
                }
 
@@ -370,16 +368,16 @@ namespace System.IO.MemoryMappedFiles
 
                protected virtual void Dispose (bool disposing)
                {
-                       if (disposing){
-                               if (stream != null){
+                       if (disposing) {
+                               if (stream != null) {
                                        if (keepOpen == false)
                                                stream.Close ();
                                        stream = null;
                                }
-                               if (handle != IntPtr.Zero) {
-                                       MemoryMapImpl.CloseMapping (handle);
-                                       handle = IntPtr.Zero;
-                               }
+                       }
+                       if (handle != null) {
+                               handle.Dispose ();
+                               handle = null;
                        }
                }