Memory Mapping error.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kunihiro
    New Member
    • Jul 2010
    • 2

    Memory Mapping error.

    I keep getting this error when I try to run my code, "Object reference not set to an instance of an object." I'm assuming something in my struct isn't done correctly because that's where the error seems to be coming from. Any help on the matter would be appreciated, Thanks.


    Code:
       public struct MMFKeyboardCommand
            {
                public int Command;
                public fixed char Alias[256];
                public fixed char Param1[256];
                public fixed char Param2[256];
                public fixed char Param3[256];
                public fixed char Param4[256];
                public fixed char Text[2048];
            }
            public struct MMFKeyboardControlMapping
            {
                public ulong NumberOfCommands;
                public bool Handled;
                [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst=32, ArraySubType= UnmanagedType.I1)]
                public MMFKeyboardCommand[] Commands;
            }
            MMFKeyboardControlMapping mmfkc = new MMFKeyboardControlMapping();
    
            private void button1_Click(object sender, EventArgs e)
            {
                int TestSize = Marshal.SizeOf(typeof(MMFKeyboardCommand));
                MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen("Test", TestSize, MemoryMappedFileAccess.ReadWriteExecute);
                MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor(0, TestSize);
                for (long i = 0; i < TestSize; i += TestSize)
                {
                    //accessor.Read(i, out _test);
                 
                    string str = "True";
             
                    fixed (char* buffer = mmfkc.Commands[0].Param1)
                    {
    
                        mmfkc.Commands[0].Command = 0x02;
    
                        memset(buffer, 0, 256);
                        for (int c = 0; c < str.Length; c++)
                        {
                            buffer[c] = str[c];
                        }
                    }
                    mmfkc.NumberOfCommands = 1;
                    mmfkc.Handled = false;
                    accessor.Write(i, ref mmfkc);
                }
    
            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by OriginalPoster
    Original Poster: "How do I fix a 'object reference not set to an instance of an object' error?
    The line your code stopped on while debugging holds all your answers.
    One of the variables/objects was created but not initialized. For example:
    Code:
    string TempString;// Created but not initialized so this is still null
    //versus
    string TempString = string.empty;
    Debug your project again. This time, when it breaks on a line look at the Locals pallet to see which variable/object is null. You can also hover your mouse over each variable and the hothelp will shows its value. One of them will be null.

    Comment

    • Kunihiro
      New Member
      • Jul 2010
      • 2

      #3
      It's this line
      Code:
       public MMFKeyboardCommand[] Commands;
      but you can't use field initializers in a struct, so I'm not sure how to initialize it.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        You init each element of the array

        Code:
        for (int Loop = 0; Loop < MaxSizeOfArray; Loop++)
        {
           MMFKeyboardCOmmand[Loop] = new Command();// Whatever you need to set the element to
        }

        Comment

        Working...