title: Access Violation Exception mystery
body: <p>I've been working with EMGU+OpenCV for quite some time and ran into this <code>AccessViolationException</code> mystery.</p>
  
  <p>First thing first, the code:</p>
  
  <pre><code>class AVE_Simulation
      {
          public static int Width = 7500;
          public static int Height = 7500;
          public static Emgu.CV.Image&lt;Rgb, float&gt;[] Images;
  
          static void Main(string[] args)
          {
              int N = 50;
              int Threads = 5;
  
              Images = new Emgu.CV.Image&lt;Rgb, float&gt;[N];
              Console.WriteLine("Start");
  
              ParallelOptions po = new ParallelOptions();
              po.MaxDegreeOfParallelism = Threads;
              System.Threading.Tasks.Parallel.For(0, N, po, new Action&lt;int&gt;((i) =&gt;
              {
                  Images[i] = GetRandomImage();
                  Console.WriteLine("Prossing image: " + i);
                  Images[i].SmoothBilatral(15, 50, 50);
                  GC.Collect();
              }));
              Console.WriteLine("End");
          }
  
          public static Emgu.CV.Image&lt;Rgb, float&gt; GetRandomImage()
          {
              Emgu.CV.Image&lt;Rgb, float&gt; im = new Emgu.CV.Image&lt;Rgb, float&gt;(Width, Height);
  
              float[, ,] d = im.Data;
              Random r = new Random((int)DateTime.Now.Ticks);
  
              for (int y = 0; y &lt; Height; y++)
              {
                  for (int x = 0; x &lt; Width; x++)
                  {
                      d[y, x, 0] = (float)r.Next(255);
                      d[y, x, 1] = (float)r.Next(255);
                      d[y, x, 2] = (float)r.Next(255);
                  }
              }
              return im;
          }
  
      }
  </code></pre>
  
  <p>The code is simple. Allocate an array of images. Generate a random image and populate it with random numbers. Execute bilateral filter over the image. That's it.</p>
  
  <p>If I execute this program in a single thread, (Threads=1) everything seems to work normally with no problem.
  But, if I raise the number of concurrent threads to 5 I get an AccessViolationException very quickly.</p>
  
  <p>I've went over OpenCV code and verified that there are no allocations on the OpenCV side and also went over the EMGU code searching for un-pinned objects or other problems and everything seems correct.</p>
  
  <p>Some notes:</p>
  
  <ol>
  <li>If you remove the <code>GC.Collect()</code> you will get the <code>AccessViolationException</code> less often but it will eventually happen.</li>
  <li>This happens only when executed in Release mode. In Debug mode I didn't experience any exceptions.</li>
  <li>Although each Image is 675MB there is no problem with allocation (I have ALLOT of memory) and a '<code>OutOfMemoryException</code>' is thrown in case the system ran out of memory.</li>
  <li>I used bilateral filter but I get this exception with other filters/functions as well. </li>
  </ol>
  
  <p>Any help would be appreciated. I've been trying to fix this for more than a week.</p>
  
  <p>i7 (no overclock), Win7 64bit, 32GB RAM, VS 2010, Framework 4.0, OpenCV 2.4.3</p>
  
  <p>Stack:</p>
  
  <pre><code>Start
  Prossing image: 20
  Prossing image: 30
  Prossing image: 40
  Prossing image: 0
  Prossing image: 10
  Prossing image: 21
  
  Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
     at Emgu.CV.CvInvoke.cvSmooth(IntPtr src, IntPtr dst, SMOOTH_TYPE type, Int32 param1, Int32 param2, Double param3, Double param4)
     at TestMemoryViolationCrash.AVE_Simulation.&lt;Main&gt;b__0(Int32 i) in C:\branches\1.1\TestMemoryViolationCrash\Program.cs:line 32
     at System.Threading.Tasks.Parallel.&lt;&gt;c__DisplayClassf`1.&lt;ForWorker&gt;b__c()
     at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
     at System.Threading.Tasks.Task.&lt;&gt;c__DisplayClass10.&lt;ExecuteSelfReplicating&gt;b__f(Object param0)
     at System.Threading.Tasks.Task.Execute()
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task&amp; currentTaskSlot)
     at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
     at System.Threading.Tasks.ThreadPoolTaskScheduler.TryExecuteTaskInline(Task task, Boolean taskWasPreviouslyQueued)
     at System.Threading.Tasks.TaskScheduler.TryRunInline(Task task, Boolean taskWasPreviouslyQueued)
     at System.Threading.Tasks.Task.InternalRunSynchronously(TaskScheduler scheduler, Boolean waitForCompletion)
     at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 loc
  alInit, Action`1 localFinally)
     at System.Threading.Tasks.Parallel.For(Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body)
     at TestMemoryViolationCrash.AVE_Simulation.Main(String[] args) in C:\branches\1.1\TestMemoryViolationCrash\Program.cs:line 35
  
  Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
     at Emgu.CV.CvInvoke.cvSmooth(IntPtr src, IntPtr dst, SMOOTH_TYPE type, Int32 param1, Int32 param2, Double param3, Double param4)
     at TestMemoryViolationCrash.AVE_Simulation.&lt;Main&gt;b__0(Int32 i) in C:\branches\1.1\TestMemoryViolationCrash\Program.cs:line 32
     at System.Threading.Tasks.Parallel.&lt;&gt;c__DisplayClassf`1.&lt;ForWorker&gt;b__c()
     at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
     at System.Threading.Tasks.Task.&lt;&gt;c__DisplayClass10.&lt;ExecuteSelfReplicating&gt;b__f(Object param0)
     at System.Threading.Tasks.Task.Execute()
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task&amp; currentTaskSlot)
     at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
     at System.Threading.ThreadPoolWorkQueue.Dispatch()
  
  Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
     at Emgu.CV.CvInvoke.cvSmooth(IntPtr src, IntPtr dst, SMOOTH_TYPE type, Int32 param1, Int32 param2, Double param3, Double param4)
     at TestMemoryViolationCrash.AVE_Simulation.&lt;Main&gt;b__0(Int32 i) in C:\branches\1.1\TestMemoryViolationCrash\Program.cs:line 32
     at System.Threading.Tasks.Parallel.&lt;&gt;c__DisplayClassf`1.&lt;ForWorker&gt;b__c()
     at System.Threading.Tasks.Task.InnerInvokeWithArg(Task childTask)
     at System.Threading.Tasks.Task.&lt;&gt;c__DisplayClass10.&lt;ExecuteSelfReplicating&gt;b__f(Object param0)
     at System.Threading.Tasks.Task.Execute()
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
     at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task&amp; currentTaskSlot)
     at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
     at System.Threading.ThreadPoolWorkQueue.Dispatch()
  Press any key to continue . . .
  </code></pre>