Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass an instance "pointer" to another process through a memory mapped file?

I'm basically searching for a way to pass instances across programs/processes without serializing the instances, in .NET 4.0.

Yes, I'm missing my good ol' 100% unsafe pointers ;)

I thought the new integration of Memory-mapped files to .NET 4.0 would help me, having read somewhere that you could pass references/pointers "natively" using it.

However, when I try something like

var mmf = MemoryMappedFile.CreateFromFile(@"C:\temp\test.mp", FileMode.Create, "mmf",
                                          1024*1024*300, 
                                          MemoryMappedFileAccess.ReadWrite);
var ss = new SimpleStruct();
ss.items = _items; //Collection of Items objects
var FileMapView = mmf.CreateViewAccessor();
FileMapView.Write<SimpleStruct>(0, ref ss); //Exception

I get the following ArgumentException:

The specified Type must be a struct containing no references.

Is it possible to pass references around using MMF? If it isn't, is there any way at all to pass instances around programs/processes?

like image 611
Simon Labrecque Avatar asked Oct 27 '25 05:10

Simon Labrecque


1 Answers

That isn't possible in unmanaged code either, a pointer value has no meaning in another process. Managed objects live on the garbage collected heap, that will never coincide with the address of a MMF view. Even if it somehow did, the garbage collector would cause havoc. Primary reasons why it took 4 versions of .NET for MMFs to become supported.

Serializing managed objects to the view is inevitable.

like image 53
Hans Passant Avatar answered Oct 29 '25 21:10

Hans Passant