Dispose() public method

public Dispose ( ) : void
return void
Ejemplo n.º 1
0
 /// <summary>
 ///     Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 /// <filterpriority>2</filterpriority>
 public void Dispose()
 {
     if (_disposed)
     {
         throw new ObjectDisposedException("Memory mapped file");
     }
     _ptr = null;
     _mmva.Dispose();
     _mmva = null;
     _mmf.Dispose();
     _mmf      = null;
     _disposed = true;
 }
Ejemplo n.º 2
0
 /// <summary>
 ///     Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 /// </summary>
 /// <filterpriority>2</filterpriority>
 public void Dispose()
 {
     if (_disposed)
     {
         throw new ObjectDisposedException("Memory mapped file");
     }
     _ptr = null;
     _mmva.SafeMemoryMappedViewHandle.ReleasePointer();
     _mmva.Dispose();
     _mmva = null;
     _mmf.Dispose();
     _mmf      = null;
     _disposed = true;
 }
Ejemplo n.º 3
0
 public FileSegment(MemoryMappedFile mmf, long offset, long length, MemoryMappedFileAccess access)
 {
     _mmf = Exceptions.CheckArgumentNull(mmf, "mmf");
     try
     {
         _length = length;
         if (_length == 0)
             _stream = new MemoryStream();
         else
             _stream = mmf.CreateViewStream(offset, length, access);
     }
     catch
     {
         _mmf.Dispose();
         throw;
     }
 }
Ejemplo n.º 4
0
        public bool IsConnected()
        {
            if (Accessor != null)
                return true;

            var dataValidEvent = Event.OpenEvent(Event.EVENT_ALL_ACCESS | Event.EVENT_MODIFY_STATE, false, "Local\\IRSDKDataValidEvent");
            if (dataValidEvent == IntPtr.Zero)
            {
                var lastError = Marshal.GetLastWin32Error();
                if (lastError == Event.ERROR_FILE_NOT_FOUND)
                    return false;

                Trace.WriteLine(string.Format("Unable to open event Local\\IRSDKDataValidEvent - Error Code {0}", lastError), "DEBUG");
                return false;
            }

            MemoryMappedFile irsdkMappedMemory = null;
            try
            {
                irsdkMappedMemory = MemoryMappedFile.OpenExisting("Local\\IRSDKMemMapFileName");
            }
            catch (Exception e)
            {
                Trace.WriteLine("Error accessing shared memory", "DEBUG");
                Trace.WriteLine(e.Message, "DEBUG");
            }

            if (irsdkMappedMemory == null)
                return false;

            var accessor = irsdkMappedMemory.CreateViewAccessor();
            if (accessor == null)
            {
                irsdkMappedMemory.Dispose();
                Trace.WriteLine("Unable to Create View into shared memory", "DEBUG");
                return false;
            }

            this.irsdkMappedMemory = irsdkMappedMemory;
            this.dataValidEvent = dataValidEvent;
            Accessor = accessor;
            return true;
        }
Ejemplo n.º 5
0
        public void Dispose()
        {
            try
            {
                if (ewh_Writer_ReadyToRead != null)
                {
                    //ewh_Writer_ReadyToRead.Set();
                    ewh_Writer_ReadyToRead.Close();
                    ewh_Writer_ReadyToRead.Dispose();
                    ewh_Writer_ReadyToRead = null;
                }
            }
            catch
            {
            }
            try
            {
                if (ewh_Writer_ReadyToWrite != null)
                {
                    //ewh_Writer_ReadyToWrite.Set();
                    ewh_Writer_ReadyToWrite.Close();
                    ewh_Writer_ReadyToWrite.Dispose();
                    ewh_Writer_ReadyToWrite = null;
                }
            }
            catch
            {
            }

            try
            {
                if (ewh_Reader_ReadyToRead != null)
                {
                    //ewh_Reader_ReadyToRead.Set();
                    ewh_Reader_ReadyToRead.Close();
                    ewh_Reader_ReadyToRead.Dispose();
                    ewh_Reader_ReadyToRead = null;
                }
            }
            catch
            {
            }
            try
            {
                if (ewh_Reader_ReadyToWrite != null)
                {
                    //ewh_Reader_ReadyToWrite.Set();
                    ewh_Reader_ReadyToWrite.Close();
                    ewh_Reader_ReadyToWrite.Dispose();
                    ewh_Reader_ReadyToWrite = null;
                }
            }
            catch
            {}

            try
            {
                if (Writer_accessor != null)
                {
                    Writer_accessor.Dispose();
                    Writer_accessor = null;
                }
            }
            catch
            {}
            try
            {
                if (Reader_accessor != null)
                {
                    Reader_accessor.Dispose();
                    Reader_accessor = null;
                }
            }
            catch
            { }
            try
            {
                if (Writer_mmf != null)
                {
                    Writer_mmf.Dispose();
                    Writer_mmf = null;
                }
            }
            catch
            { }
            try
            {
                if (Reader_mmf != null)
                {
                    Reader_mmf.Dispose();
                    Reader_mmf = null;
                }
            }
            catch
            { }
        }
        public DumpFileMemoryReader(string dumpFilePath)
        {
            bool dispose = true;
            FileStream fileStream = null;
            MemoryMappedFile memoryMappedFile = null;
            MemoryMappedViewStream stream = null;

            try
            {
                fileStream = new FileStream(dumpFilePath, FileMode.Open, FileAccess.Read);
                memoryMappedFile = MemoryMappedFile.CreateFromFile(fileStream, Guid.NewGuid().ToString(), fileStream.Length, MemoryMappedFileAccess.Read, new MemoryMappedFileSecurity(), HandleInheritability.Inheritable, false);
                stream = memoryMappedFile.CreateViewStream(0, fileStream.Length, MemoryMappedFileAccess.Read);

                stream.SafeMemoryMappedViewHandle.AcquirePointer(ref basePointer);
                IntPtr streamPointer = IntPtr.Zero;
                uint streamSize = 0;
                MINIDUMP_DIRECTORY directory = new MINIDUMP_DIRECTORY();

                if (!MiniDumpReadDumpStream((IntPtr)basePointer, MINIDUMP_STREAM_TYPE.Memory64ListStream, ref directory, ref streamPointer, ref streamSize))
                    throw new Exception("Unable to read mini dump stream");

                var data = (MINIDUMP_MEMORY64_LIST)Marshal.PtrToStructure(streamPointer, typeof(MINIDUMP_MEMORY64_LIST));
                ulong lastEnd = data.BaseRva;

                ranges = new MemoryLocation[data.NumberOfMemoryRanges];
                for (int i = 0; i < ranges.Length; i++)
                {
                    var descriptor = (MINIDUMP_MEMORY_DESCRIPTOR64)Marshal.PtrToStructure(streamPointer + sizeof(MINIDUMP_MEMORY64_LIST) + i * sizeof(MINIDUMP_MEMORY_DESCRIPTOR64), typeof(MINIDUMP_MEMORY_DESCRIPTOR64));
                    ranges[i] = new MemoryLocation()
                    {
                        MemoryStart = descriptor.StartOfMemoryRange,
                        MemoryEnd = descriptor.StartOfMemoryRange + descriptor.DataSize,
                        FilePosition = lastEnd,
                    };
                    lastEnd += descriptor.DataSize;
                }

                int newEnd = 0;
                for (int i = 1; i < ranges.Length; i++)
                    if (ranges[i].MemoryStart == ranges[newEnd].MemoryEnd)
                        ranges[newEnd].MemoryEnd = ranges[i].MemoryEnd;
                    else
                        ranges[++newEnd] = ranges[i];
                newEnd++;
                Array.Resize(ref ranges, newEnd);
                finder = new MemoryRegionFinder(ranges.Select(r => new MemoryRegion { BaseAddress = r.MemoryStart, MemoryEnd = r.MemoryEnd }).ToArray());
                dispose = false;
            }
            finally
            {
                if (dispose)
                {
                    stream.SafeMemoryMappedViewHandle.ReleasePointer();
                    if (stream != null)
                    {
                        stream.Dispose();
                    }

                    if (memoryMappedFile != null)
                    {
                        memoryMappedFile.Dispose();
                    }

                    if (fileStream != null)
                    {
                        fileStream.Dispose();
                    }
                }
                else
                {
                    this.fileStream = fileStream;
                    this.stream = stream;
                    this.memoryMappedFile = memoryMappedFile;
                }
            }
        }
Ejemplo n.º 7
0
        private void read_events_thread() {

            string prefix = use_global_ ? "Global\\" : "";
            try {
                memory_file_ = MemoryMappedFile.CreateNew(prefix + "DBWIN_BUFFER", 4096L);

                bool created = false;
                buffer_ready_ = new EventWaitHandle( false, EventResetMode.AutoReset, prefix + "DBWIN_BUFFER_READY", out created);
                if (!created) 
                    errors_.add("Can't create the DBWIN_BUFFER_READY event/" + use_global_);

                if (created) {
                    data_ready_ = new EventWaitHandle(false, EventResetMode.AutoReset, prefix + "DBWIN_DATA_READY", out created);
                    if (!created) 
                        errors_.add("Can't create the DBWIN_DATA_READY event/" + use_global_);
                }

                if (created) {
                    buffer_ready_.Set();
                    while (!disposed_) {
                        if (!data_ready_.WaitOne(1000))
                            continue;

                        using (var stream = memory_file_.CreateViewStream()) {
                            using (var reader = new BinaryReader(stream, Encoding.Default)) {
                                var process_id = (int)reader.ReadUInt32();
                                var raw = reader.ReadChars(4092);
                                var idx = Array.IndexOf(raw, '\0');
                                var msg = new string(raw, 0, idx);
                                find_process_id(process_id);
                                string process_name = pid_to_name_.ContainsKey(process_id) ? pid_to_name_[process_id] : "";
                                lock (this)
                                    events_.Add(new debug_event {
                                        unique_id = next_unique_id++, process_id = process_id, msg = msg, lo_process_name = process_name
                                    });
                            }
                        }
                
                        buffer_ready_.Set();
                    }
                }
            } catch (Exception e) {
                logger.Fatal("Can't read debug events " + e.Message);
                errors_.add("Error reading debug events " + e.Message);
            }

            if ( memory_file_ != null)
                memory_file_.Dispose();
            if ( data_ready_ != null)
                data_ready_.Dispose();
            if ( buffer_ready_ != null)
                buffer_ready_.Dispose();

        }
Ejemplo n.º 8
0
 private void InitFromStream(Stream input, uint length)
 {
     m_map = MemoryMappedFile.CreateNew (null, length, MemoryMappedFileAccess.ReadWrite,
         MemoryMappedFileOptions.None, null, HandleInheritability.None);
     try
     {
         using (var view = m_map.CreateViewAccessor (0, length, MemoryMappedFileAccess.Write))
         {
             var buffer = new byte[81920];
             unsafe
             {
                 byte* ptr = view.GetPointer (0);
                 try
                 {
                     uint total = 0;
                     while (total < length)
                     {
                         int read = input.Read (buffer, 0, buffer.Length);
                         if (0 == read)
                             break;
                         read = (int)Math.Min (read, length-total);
                         Marshal.Copy (buffer, 0, (IntPtr)(ptr+total), read);
                         total += (uint)read;
                     }
                     MaxOffset = total;
                 }
                 finally
                 {
                     view.SafeMemoryMappedViewHandle.ReleasePointer();
                 }
             }
         }
         View = new Frame (this);
     }
     catch
     {
         m_map.Dispose();
         throw;
     }
 }
Ejemplo n.º 9
0
 private void InitFromFileStream(FileStream fs, uint length)
 {
     m_map = MemoryMappedFile.CreateFromFile (fs, null, length,
         MemoryMappedFileAccess.Read, null, HandleInheritability.None, true);
     try {
         View = new Frame (this);
     } catch {
         m_map.Dispose(); // dispose on error only
         throw;
     }
 }
Ejemplo n.º 10
0
 static void Uninitialize(string identifier, Mutex mutex, MemoryMappedFile file, long offset)
 {
     try {
         if (!mutex.WaitOne(LOCK_TIMEOUT)) {
             throw new InvalidOperationException("Unable to uninitialize inter-process communication");
         }
     } catch (AbandonedMutexException) {
         mutex.Dispose();
         file.Dispose();
         return;
     }
     try {
         Data me;
         if (identifier != null) {
             using (var accessor = file.CreateViewAccessor(offset, Marshal.SizeOf(typeof(Data)))) {
                 accessor.Read(0, out me);
                 me.InUse = false;
                 accessor.Write(0, ref me);
             }
         }
     } finally {
         mutex.ReleaseMutex();
         mutex.Dispose();
         file.Dispose();
     }
 }
Ejemplo n.º 11
0
        static void Initialize(string identifier, out Mutex mutex, out MemoryMappedFile file, out long offset, out Data data)
        {
            data = new Data {
                Identifier = identifier,
                Initialized = true,
                InUse = true,
                OwnerProcessId = Process.GetCurrentProcess().Id,
                ItemsInQueue = int.MaxValue,
                MaximumItems = 0,
                Message = null
            };
            Data empty = new Data {
                Identifier = string.Empty,
                Initialized = false,
                InUse = false
            };
            int SIZE = Marshal.SizeOf(data);

            bool isNew;
            try {
                mutex = new Mutex(true, MUTEX_NAME, out isNew);
            } catch (UnauthorizedAccessException) {
                mutex = Mutex.OpenExisting(MUTEX_NAME);
                isNew = false;
            }
            offset = 0;

            if (!isNew && !mutex.WaitOne(LOCK_TIMEOUT)) {
                mutex.Dispose();
                throw new InvalidOperationException("Unable to initialize inter-process communication");
            }

            file = null;
            try {
                if (isNew) {
                    file = MemoryMappedFile.CreateNew(MMF_NAME, (MAX_ITEMS + 2) * SIZE,
                        MemoryMappedFileAccess.ReadWrite,
                        MemoryMappedFileOptions.None,
                        null,
                        System.IO.HandleInheritability.None);

                    if (identifier != null) {
                        using (var accessor = file.CreateViewAccessor(offset, SIZE * 2)) {
                            accessor.Write(0, ref data);
                            // Zero the following entry
                            accessor.Write(SIZE, ref empty);
                        }
                    } else {
                        using (var accessor = file.CreateViewAccessor(offset, SIZE)) {
                            // Zero the first entry
                            accessor.Write(0, ref empty);
                        }
                    }
                } else {
                    file = MemoryMappedFile.OpenExisting(MMF_NAME, MemoryMappedFileRights.ReadWrite);

                    if (identifier != null) {
                        bool succeeded = false;
                        for (int index = 0; index < MAX_ITEMS; ++index) {
                            Data existing;
                            offset = index * SIZE;
                            using (var accessor = file.CreateViewAccessor(offset, SIZE * 2)) {
                                accessor.Read(0, out existing);
                                if (!existing.Initialized) {
                                    accessor.Write(0, ref data);
                                    // This wasn't initialized, so zero the following entry
                                    accessor.Write(SIZE, ref empty);
                                    succeeded = true;
                                    break;
                                } else if (!existing.InUse) {
                                    accessor.Write(0, ref data);
                                    // Initialized is true, so the next entry is zeroed already
                                    succeeded = true;
                                    break;
                                } else if (existing.Identifier == identifier) {
                                    throw new IdentifierInUseException("Identifier is already in use");
                                }
                            }
                        }
                        if (!succeeded) {
                            throw new InvalidOperationException("Out of space for entries");
                        }
                    }
                }
            } catch {
                mutex.ReleaseMutex();
                mutex.Dispose();
                mutex = null;
                if (file != null) {
                    file.Dispose();
                    file = null;
                }
                throw;
            } finally {
                if (mutex != null) {
                    mutex.ReleaseMutex();
                }
            }
        }
        public MemoryMappedIndexInput(string file)
        {
            this._length = new FileInfo(file).Length;

            _mmf = MemoryMappedFile.CreateFromFile(file, FileMode.Open);
            try
            {
                _accessor = _mmf.CreateViewAccessor(0, _length);
            }
            catch (IOException ex)
            {
                _mmf.Dispose();
                throw ex;
            }
        }