title: Emgu AccessViolationException randomly occurring after upgrading from Emgu 2 to Emgu 3.1.0.1
body: <p>I was using Emgu 2 (the one using opencv 2.4.10.1) and that was running pretty stable and would run without crashing. I have now upgrade to Emgu 3.1.0.1 and my app will crash sometimes within hours or a day with an AccessViolationException. I have seen it crash at two different locations now. See the two locations marked in the source code below (CRASH1 &amp; CRASH2). So I think there is something fundamentally wrong here.</p>
  
  <p>I am running a Release version of the software. Any one got any ideas what might be going on here?</p>
  
  <p>The CRASH1 exception is:</p>
  
  <pre><code>Framework Version: v4.0.30319
  Description: The process was terminated due to an unhandled exception.
  Exception Info: System.AccessViolationException
     at Emgu.CV.CvInvoke.cveSubtract(IntPtr, IntPtr, IntPtr, IntPtr, Emgu.CV.CvEnum.DepthType)
     at Emgu.CV.CvInvoke.Subtract(Emgu.CV.IInputArray, Emgu.CV.IInputArray, Emgu.CV.IOutputArray, Emgu.CV.IInputArray, Emgu.CV.CvEnum.DepthType)
     at Emgu.CV.RotationMatrix2D.CreateRotationMatrix(System.Drawing.PointF, Double, System.Drawing.Size, System.Drawing.Size ByRef)
     at Emgu.CV.Image`2[[Emgu.CV.Structure.Bgr, Emgu.CV.World, Version=3.1.0.2282, Culture=neutral, PublicKeyToken=7281126722ab4438],[System.Byte, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Rotate(Double, System.Drawing.PointF, Emgu.CV.CvEnum.Inter, Emgu.CV.Structure.Bgr, Boolean)
     at Emgu.CV.Image`2[[Emgu.CV.Structure.Bgr, Emgu.CV.World, Version=3.1.0.2282, Culture=neutral, PublicKeyToken=7281126722ab4438],[System.Byte, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Rotate(Double, Emgu.CV.Structure.Bgr, Boolean)
     at XXXX.ProcessFrames()
     at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <p>The CRASH2 exception is:</p>
  
  <pre><code>Framework Version: v4.0.30319
  Description: The process was terminated due to an unhandled exception.
  Exception Info: System.AccessViolationException
     at Emgu.CV.Util.VectorOfMat.VectorOfMatPush(IntPtr, IntPtr)
     at Emgu.CV.Util.VectorOfMat..ctor(Emgu.CV.Mat[])
     at XXXX.ProcessFrames()
     at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
     at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
     at System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <p>The source code is:</p>
  
  <pre><code>    public void start()
      {
          started = true;
          // start processing camera frames
          cameraThread = new Thread(new ThreadStart(ProcessFrames));
          cameraThread.IsBackground = true;
          cameraThread.Start();
      }
  
      private void ProcessFrames()
      {
          Bgr frameBg = new Bgr(0,0,0);
          while (true)
          {
              // try to open the camera device
              if (capture == null)
              {
                  try
                  {
                      int index;
                      bool isNumeric = int.TryParse(ConfigurationManager.AppSettings["CameraSource"], out index);
                      capture = isNumeric ? new Capture(index) : new Capture(ConfigurationManager.AppSettings["CameraSource"]);
                      capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth, 640);
                      capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight, 480);
                      capture.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FourCC, Emgu.CV.VideoWriter.Fourcc('M', 'J', 'P', 'G'));
                  }
                  catch
                  {
                      frameObservers.ForEach(a =&gt; a.frame(cameraNotConnectedBitmap, new List&lt;Person&gt;(), recognizer.isActive()));
                  }
                  Task.Delay(1000).Wait();
                  continue;
              }
  
              // read a frame from the camera
              Mat matFrame = capture.QueryFrame();
              Image&lt;Bgr, byte&gt; frame = null;
              if (matFrame != null)
              {
                  frame = matFrame.ToImage&lt;Bgr, byte&gt;();
              }
              if (frame == null)
              {
                  frameObservers.ForEach(a =&gt; a.frame(cameraNotConnectedBitmap, new List&lt;Person&gt;(), recognizer.isActive()));
                  capture = null; // force to reconnect to camera again
                  Task.Delay(1000).Wait();
                  continue;
              }
  
              try
              {
                  CRASH1 &gt;&gt;&gt;frame = frame.Rotate(Convert.ToInt32(ConfigurationManager.AppSettings["CameraRotation"]), frameBg, false);
                  double scale = Convert.ToDouble(ConfigurationManager.AppSettings["CameraScale"]);
                  if (scale &gt; 0)
                  {
                      frame = frame.Resize(scale, Emgu.CV.CvEnum.Inter.Linear);
                  }
  
                  Global.FRAME_HEIGHT = frame.Height;
                  Global.FRAME_WIDTH = frame.Width;
                  rawCameraFeedFrameObservers.ForEach(a =&gt; a.frame(frame));
  
                  if (autoBrightness)
                  {
                      Image&lt;Lab, Byte&gt; lab = frame.Convert&lt;Lab, Byte&gt;();
                      Image&lt;Gray, Byte&gt;[] planes = lab.Split();
                      CvInvoke.CLAHE(planes[0], claheClipLimit, new Size(8, 8), planes[0]);
                      CRASH2 &gt;&gt;&gt;VectorOfMat vm = new VectorOfMat(planes[0].Mat, planes[1].Mat, planes[2].Mat); 
                      CvInvoke.Merge(vm, lab);
                      frame = lab.Convert&lt;Bgr, Byte&gt;();
                  } 
              }
              catch
              {
                  // not the end of the world
                  Log.INFO("INFO", "Processing the frame failed, skipping this frame");
                  continue;
              }
          }
      }
  </code></pre>