The following Unit Test (NUnit Framework) fails when run in NCrunch but passes when run in other test runners.
Code:
[Test]
public void weak_reference_test()
{
var weakReference = new WeakReference(new Object());
GC.Collect();
Assert.IsFalse(weakReference.IsAlive);
}
The issue here is that NCrunch instrumentation appears to be keeping the new Object() alive (it is GC rooted as a local/parameter when run in NCrunch according to memory profiler).
If I disable NCrunch instrumentation for the assembly then it passes in NCrunch. Interestingly, when I run the
instrumented assembly in the NUnit test runner it passes, suggesting that it isn't *just* the instrumentation that is the problem but something else that NCrunch is doing with the assembly beyond the instrumentation.
Unfortunately, I heavily leverage the code coverage metrics provided by NCrunch (in fact, it is one of the primary features I use it for) so disabling instrumentation on the assembly isn't an option for me.
NOTE: The above test is the most simplified example of the thing I am testing. In reality, I have a more complex class that is being tested and I need to test to make sure that the class isn't holding onto strong references to objects as this is critical for the class to function correctly.
UPDATE: Disabling "Analyze Execution Times" causes the example above to pass.