title: mysterious array keeps reference to object in unit tests
body: <p>I just observed a really weird behavior in Visual Studio 2013 Update 4. Consider the following xunit.net test:</p>
  
  
  
  <pre><code>[Fact]
  public void TestTargetMustNotHoldAReferenceToItemsAfterCallingClear()
  {
      var item = new MarkedType();
      var weakReferenceToItem = new WeakReference&lt;MarkedType&gt;(item);
      var testTarget = new ListBuilder&lt;MarkedType&gt;().WithItems(item)
                                                    .Build();
  
      testTarget.Clear();
  
      item = null;
      GC.Collect();
      MarkedType retrievedItem = null;
      Assert.False(weakReferenceToItem.TryGetTarget(out retrievedItem));
      Assert.Null(retrievedItem);
  }
  
  public class MarkedType { }
  </code></pre>
  
  <p>In it, I want to check that my own <code>List&lt;T&gt;</code> implementation does not hold references to objects on the heap after I call <code>Clear</code> on it. Therefore I set <code>item</code> to <code>null</code> after the call to <code>Clear</code> and let the Garbage Collector run so that it can deallocate the <code>MarkedType</code> instance. Afterwards I check with a weak reference that the instance is really gone.</p>
  
  <p><strong>Here is the weird thing:</strong> this test does not pass in Debug mode in VS 2013 Update 4 with xunit.net v2.0.0 prerelease because <code>WeakReference.TryGetTarget</code> returns <code>true</code>. <strong>However, if I switch to Release mode, this test passes.</strong> I also ran this test in VS 2015 CTP6 and there it passes in both configurations, Debug and Release.</p>
  
  <p>I then created two memory dumps (one before the garbage collector run, one after) to figure out what object keeps <code>item</code> in memory and I found an object array that is not listed in my test:</p>
  
  <p><img src="https://i.stack.imgur.com/L7h3V.png" alt="Marked Type Path To Root"></p>
  
  <p><strong>My actual questions are: where does this object array come from? Why isn't it present in Release mode (or VS 2015)? Can I somehow disable or circumvent it?</strong></p>
  
  <p>If you want to play around with the source code, it is available on github (please use the FailingReferenceTest branch): <a href="https://github.com/feO2x/BeginningTdd" rel="nofollow noreferrer">https://github.com/feO2x/BeginningTdd</a></p>