title: Visual Studio C# Attempted to read or write protected memory. This is often an indication that other memory is corrupt
body: <p>I am creating an Attendance System using 4 cameras for facial recognition. I am using Emgu CV 3.0 in C#. Now, in my attendance form, which consist of 4 imagebox, the application suddenly stops and it goes back to the main form and shows an error to the button which reference the attendance form. The error was:</p>
  
  <p>Attempted to read or write protected memory. This is often an indication that other memory is corrupt.</p>
  
  <p>Here is the code where the error occured:</p>
  
  <pre><code>    private void btn_attendance_Click(object sender, EventArgs e)
      {
          attendance attendance = new attendance();
          attendance.ShowDialog();
      }
  </code></pre>
  
  <p>Here is the code for the Attendance form without the recognition part:</p>
  
  <pre><code>public partial class attendance : Form
  {
      private Capture cam1, cam2, cam3, cam4;
      private CascadeClassifier _cascadeClassifier;
      private RecognizerEngine _recognizerEngine;
      private String _trainerDataPath = "\\traineddata_v2";
      private readonly String dbpath = "Server=localhost;Database=faculty_attendance_system;Uid=root;Pwd=root;";
      MySqlConnection conn;
  
      public attendance()
      {
         InitializeComponent();
          conn = new MySqlConnection("Server=localhost;Database=faculty_attendance_system;Uid=root;Pwd=root;");
      }
  
      private void btn_home_Click(object sender, EventArgs e)
      {
          this.Close();
      }
  
      private void attendance_Load(object sender, EventArgs e)
      {
          time_now.Start();
          lbl_date.Text = DateTime.Now.ToString("");
          _recognizerEngine = new RecognizerEngine(dbpath, _trainerDataPath);
  
          _cascadeClassifier = new CascadeClassifier(Application.StartupPath + "/haarcascade_frontalface_default.xml");
          cam1 = new Capture(0);
          cam2 = new Capture(1);
          cam3 = new Capture(3);
          cam4 = new Capture(4);
  
          Application.Idle += new EventHandler(ProcessFrame);
      }
  
      private void ProcessFrame(Object sender, EventArgs args)
      {
          Image&lt;Bgr, byte&gt; nextFrame_cam1 = cam1.QueryFrame().ToImage&lt;Bgr, Byte&gt;();
          Image&lt;Bgr, byte&gt; nextFrame_cam2 = cam2.QueryFrame().ToImage&lt;Bgr, Byte&gt;();
          Image&lt;Bgr, byte&gt; nextFrame_cam3 = cam3.QueryFrame().ToImage&lt;Bgr, Byte&gt;();
          Image&lt;Bgr, byte&gt; nextFrame_cam4 = cam4.QueryFrame().ToImage&lt;Bgr, Byte&gt;();
  
          using (nextFrame_cam1)
          {
             if (nextFrame_cam1 != null)
              {
                  Image&lt;Gray, byte&gt; grayframe = nextFrame_cam1.Convert&lt;Gray, byte&gt;();
                  var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
                  foreach (var face in faces)
                  {
                      nextFrame_cam1.Draw(face, new Bgr(Color.Green), 3);
                      var predictedUserId = _recognizerEngine.RecognizeUser(new Image&lt;Gray, byte&gt;(nextFrame_cam1.Bitmap));
                  }
                  imageBox1.Image = nextFrame_cam1;
              }
          }
  
          using (nextFrame_cam2)
          {
             if (nextFrame_cam2!= null)
              {
                  Image&lt;Gray, byte&gt; grayframe = nextFrame_cam2.Convert&lt;Gray, byte&gt;();
                  var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
                  foreach (var face in faces)
                  {
                      nextFrame_cam2.Draw(face, new Bgr(Color.Green), 3);
                      var predictedUserId = _recognizerEngine.RecognizeUser(new Image&lt;Gray, byte&gt;(nextFrame_cam2.Bitmap));
                  }
                  imageBox2.Image = nextFrame_cam2;
              }
          }
  
  
          using (nextFrame_cam3)
          {
             if (nextFrame_cam3!= null)
              {
                  Image&lt;Gray, byte&gt; grayframe = nextFrame_cam3.Convert&lt;Gray, byte&gt;();
                  var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
                  foreach (var face in faces)
                  {
                      nextFrame_cam3.Draw(face, new Bgr(Color.Green), 3);
                      var predictedUserId = _recognizerEngine.RecognizeUser(new Image&lt;Gray, byte&gt;(nextFrame_cam3.Bitmap));
                  }
                  imageBox3.Image = nextFrame_cam3;
              }
          }
  
          using (nextFrame_cam4)
          {
             if (nextFrame_cam4!= null)
              {
                  Image&lt;Gray, byte&gt; grayframe = nextFrame_cam4.Convert&lt;Gray, byte&gt;();
                  var faces = _cascadeClassifier.DetectMultiScale(grayframe, 1.5, 10, Size.Empty, Size.Empty);
                  foreach (var face in faces)
                  {
                      nextFrame_cam4.Draw(face, new Bgr(Color.Green), 3);
                      var predictedUserId = _recognizerEngine.RecognizeUser(new Image&lt;Gray, byte&gt;(nextFrame_cam4.Bitmap));
                  }
                  imageBox4.Image = nextFrame_cam4;
              }
          }
      }
  }
  </code></pre>