title: Weird Access Violation Exception
body: <p>I'm puzzled with an occurance of <code>AccessViolationException</code>. It's <s>quite im</s>possible (see answer) to have a clean reproduction but here goes the general idea:</p>
  
  <pre><code>class MyClass 
  {
    public List&lt;SomeType&gt; MyMethod(List&lt;string&gt; arg)
    {
       // BREAKPOINT here
       // Simple stuff here, nothing fancy, no external libs used
    }
  }
  
  delegate List&lt;SomeType&gt; MyDelegate(List&lt;string&gt; arg);
  
  ... 
  
  var myObject = new MyClass();
  
  Func&lt;List&lt;string&gt;, List&lt;SomeType&gt;&gt; myFunc = myObject.MyMethod;
  MyDelegate myDelegate = myObject.MyMethod;
  
  myFunc(null)            // works fine
  myDelegate(null)        // works fine
  myObject.MyMethod(null) // throws AccessViolationException
  </code></pre>
  
  <p>The weird part is I'm not using any unsafe code. I don't have any dependencies on external libraries anywhere close (and anywhere in the whole program execution AFAIK).</p>
  
  <p>The weirdest part is this is 100% reproducable and even after refactoring the code slightly, moving the method invocation elsewhere, putting extra code before it etc. - in all cases <code>AccessViolationException</code> is still thrown on that particular method invocation. The method is never entered when invoked directly (breakpoint is not hit). Invoking it through delegate or <code>Func&lt;&gt;</code> works fine.</p>
  
  <p>Any clues as to what could cause it or how to debug it?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Following antiduh's question: There is no call to a virtual method from a constructor anywhere close. The actual stack trace when this happens is very simple, just two static methods and a simple instance one.</p>
  
  <p>The only clue seems to be <strong>threading</strong>. There is <code>Parallel.ForEach()</code> and <code>Thread.Sleep()</code> invoked <em>before</em> this in program execution (not in call stack). Any clues as to how mishandled threading (with regular, managed classes) could cause AVE?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Narrowed it down to a VS bug, see my answer.</p>