Effectively release unmanaged resources when process is killed

184 Views Asked by At

I have multiple processes (or rather multiple instances of a process) trying to (create or) access a single memory-mapped-file. I'm using Mutex to synchronize (first) creation of mmap file.

When all the processes run the first time, it works well and data is read properly. But if I close all the processes (as they take a long time) by 'right click -> close all windows' and start the processes again immediately, I face issues like System.IO.IOException: Pipe is broken. The unmanaged resources are not cleaned properly in time. I'm using dispose() method towards the end of the program.

MemoryMappedFile mmf;
Mutex mutex = new Mutex(false, mmapName.Substring(2));
mutex.WaitOne();
try
{  
    mmf = MemoryMappedFile.OpenExisting(mmapName);
}
catch (FileNotFoundException)
{
    mmf = MemoryMappedFile.CreateFromFile(rFile, FileMode.Open, mmapName);
}
mutex.ReleaseMutex();
MemoryMappedViewStream fileStream = mmf.CreateViewStream(0, 0, 
MemoryMappedFileAccess.Read);
...

Is there a way to resolve this issue? 'using' keyword wouldn't help. Should I use SafeHandles? (I'm not experienced with SafeHandles but there are some SafeMemoryMappedFileHandle / SafeMemoryMappedViewHandles already provided.)

0

There are 0 best solutions below