title: Crashing: Attempted to read or write protected memory c#
body: <p>We have a periodic crashing problem with our software. I think I have tracked down where it is happening but I am unfamiliar with the code. It's extremely difficult to debug.</p>
  
  <p>We cannot reproduce this problem on our development environment, it happens on our live environment. The users log in to our system with swipe cards, the terminals have magnetic swipe readers. It happens maybe once a day when they swipe a card. The MSR is read like a keyboard.</p>
  
  <p>I have swiped a card a few hundred times in a row on different occasions and have had no issues on dev.</p>
  
  <p>There is try catch but it doesn't catch anything, the error we are getting in event viewer is:</p>
  
  <p>"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."</p>
  
  <p>Here is the function I am pretty sure it's crashing in:</p>
  
  <pre><code>    private void GetRawInputInfo(
          IntPtr rawInputHeader,
          ref IntPtr deviceHandle,
          ref bool handled,
          ref StringBuilder buffer)
      {
          try
          {
              uint cbSize = 0;
              IntPtr hRawInput;
  
              hRawInput = rawInputHeader;
  
  
              if (UnsafeNativeMethods.GetRawInputData(
                  hRawInput,
                  Structures.RAWINPUTDATAUICOMMAND.RID_INPUT,
                  IntPtr.Zero,
                  ref cbSize,
                  (uint)Marshal.SizeOf(typeof(Structures.RAWINPUTHEADER))) == 0)
              {
                  IntPtr ptr = Marshal.AllocHGlobal((int)cbSize);
  
                  if (ptr != IntPtr.Zero &amp;&amp;
                      UnsafeNativeMethods.GetRawInputData(
                          hRawInput,
                          Structures.RAWINPUTDATAUICOMMAND.RID_INPUT,
                          ptr,
                          ref cbSize,
                          (uint)Marshal.SizeOf(typeof(Structures.RAWINPUTHEADER))) == cbSize)
                  {
  
                      Structures.RAWINPUT raw = (Structures.RAWINPUT)Marshal.PtrToStructure(ptr, typeof(Structures.RAWINPUT));
  
                      deviceHandle = raw.header.hDevice;
                      handled = raw.header.dwType == Structures.RAWINPUTDEVICEDWTYPE.RIM_TYPEKEYBOARD &amp;&amp;
                          raw.keyboard.Message == Messages.WM_KEYDOWN;
  
                      if (handled)
                      {
                          byte[] state = new byte[256];
  
                          // Force the keyboard status cache to update
                          UnsafeNativeMethods.GetKeyState(0);
  
                          // Note: GetKeyboardState only returns valid state when
                          // the application has focus -- this is why we weren't
                          // getting shift keys when the application was not focused
                          if (UnsafeNativeMethods.GetKeyboardState(state))
                          {
                              //StringBuilder unmanagedBuffer = new StringBuilder(64);
  
                              if (UnsafeNativeMethods.ToUnicode(
                                  raw.keyboard.VKey,
                                  raw.keyboard.MakeCode,
                                  state,
                                  buffer,
                                  64,
                                  0) &lt;= 0)
                              {
                                  buffer.Remove(0, buffer.Length);
                              }
                          }
                      }
                  }
                  if (ptr != IntPtr.Zero)
                  {
                      Marshal.FreeHGlobal(ptr);
                  }
              }
          }
          catch (Exception e)
          {
              BLog.LogError("Error running: GetRawInputInfo()" + e.Message);
          }
      }
  </code></pre>
  
  <p>Any help or pointers (pun intended) in the right direction would be appreciated,</p>
  
  <p>Thanks</p>
  
  <p><strong>----Updated:</strong></p>
  
  <p>Here is the call stack if this helps in any way:</p>
  
  <p>Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</p>
  
  <p>at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&amp; msg)</p>
  
  <p>at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)</p>
  
  <p>at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)</p>
  
  <p>at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)</p>
  
  <p>at System.Windows.Forms.Application.RunDialog(Form form)</p>
  
  <p>at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)</p>
  
  <p>at System.Windows.Forms.Form.ShowDialog()</p>
  
  <p>at MyApplication.UI.BaseDisplay.ShowDialog(SecurityData security, Int32 listViewState, Nullable`1 ticketId, Int32 currentSeatPosition)</p>
  
  <p>I have made the changes as per Matt and will see if there is any change in the crashing but it may take a day or 2. Sometimes it doesn't crash every day, but generally there is one every day.</p>