Created
September 29, 2020 08:05
-
-
Save Const-me/9ae865dd94df0a7423aa054cf76bf101 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO.MemoryMappedFiles; | |
using System.Runtime.InteropServices; | |
namespace MemoryMapBug | |
{ | |
[StructLayout( LayoutKind.Sequential, Pack = 1 )] | |
struct sTest | |
{ | |
public int f1; | |
public byte f2; | |
} | |
static class Program | |
{ | |
const int length = 1024 * 64; | |
static void test() | |
{ | |
int cb = Marshal.SizeOf( typeof( sTest ) ); | |
Debug.Assert( cb == 5 ); | |
MemoryMappedFile mmf = MemoryMappedFile.CreateNew( null, cb * length ); | |
var acc = mmf.CreateViewAccessor(); | |
// Console.WriteLine( "Capacity: {0}", acc.Capacity ); | |
sTest[] arr = new sTest[ length ]; | |
for( int i = 0; i < length; i++ ) | |
arr[ i ] = new sTest() { f1 = i, f2 = (byte)( i & 0xFF ) }; | |
acc.WriteArray( 0, arr, 0, length ); | |
} | |
static void Main( string[] args ) | |
{ | |
try | |
{ | |
test(); | |
Console.WriteLine( "Completed OK" ); | |
} | |
catch( Exception ex ) | |
{ | |
Console.WriteLine( "Crashed: {0}", ex.Message ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment