Skip to content

Added Finalizer to MemoryMappedFile #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/absil/ilread.fs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ let derefByte (p:nativeint) =
NativePtr.read (NativePtr.ofNativeInt<byte> p)

type MemoryMappedFile(hMap: MemoryMapping.HANDLE, start:nativeint) =
let mutable disposed = false

static member Create fileName =
//printf "fileName = %s\n" fileName;
Expand All @@ -162,7 +163,7 @@ type MemoryMappedFile(hMap: MemoryMapping.HANDLE, start:nativeint) =

if start.Equals(IntPtr.Zero) then
failwithf "MapViewOfFile(0x%08x)" ( Marshal.GetHRForLastWin32Error() );
MemoryMappedFile(hMap, start)
new MemoryMappedFile(hMap, start)

member m.Addr (i:int) : nativeint =
start + nativeint i
Expand All @@ -181,9 +182,7 @@ type MemoryMappedFile(hMap: MemoryMapping.HANDLE, start:nativeint) =
member m.ReadUInt16 i =
NativePtr.read (NativePtr.ofNativeInt<uint16> (m.Addr i))

member m.Close() =
ignore(MemoryMapping.UnmapViewOfFile start);
ignore(MemoryMapping.CloseHandle hMap)
member m.Close() = m.Dispose()

member m.CountUtf8String i =
let start = m.Addr i
Expand All @@ -196,6 +195,20 @@ type MemoryMappedFile(hMap: MemoryMapping.HANDLE, start:nativeint) =
let n = m.CountUtf8String i
new System.String(NativePtr.ofNativeInt (m.Addr i), 0, n, System.Text.Encoding.UTF8)

override m.Finalize() = m.Dispose(false)

member m.Dispose(_ : bool) =
if not disposed then
ignore(MemoryMapping.UnmapViewOfFile start);
ignore(MemoryMapping.CloseHandle hMap)
disposed <- true

member m.Dispose() =
m.Dispose(true)
GC.SuppressFinalize(m)

interface IDisposable with
override m.Dispose() = m.Dispose()

type MMapChannel =
{ mutable mmPos: int;
Expand Down