Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Why is this weakref test failing according to NCrunch but passing according to visual studio?
Strilanc
#1 Posted : Thursday, January 3, 2013 5:06:14 PM(UTC)
Rank: Member

Groups: Registered
Joined: 4/3/2012(UTC)
Posts: 17

Thanks: 1 times
NCrunch seems to be preventing the garbage collection of objects that only have weak references.

Code:
    [TestMethod]
    public void WeakReferencesCanBeCollected() {
        var r = new object();
        var w = new WeakReference<object>(r);
        r = null;
        GC.Collect();
        Assert.IsFalse(w.TryGetTarget(out r)); // NCrunch says fails here, but MSTest passes it
    }


This also happens when debugging. If I use NCrunch's "Debug covering test" it throws, but if I use visual studio's "Debug test" then it passes.

My best guess is that I'm relying on undefined behavior. The language semantics might not require the garbage collector to clean up everything, despite being asked for a full collection. I'm pretty sure that the debugger prolongs the life of local variables (technically they can be collected after last use, but the debugger keeps them around until the end of their scope [see: GC.KeepAlive]), and maybe something similar is happening.

The reason I want this sort of test is to test for particular runtime optimizations. For example, list.Skip(2).Skip(2) should be optimized into list.Skip(4) and this can be detected from the outside by the collection of the intermediate list. There's a few of these tests in https://github.com/Strilanc/LinqToCollections .
Remco
#2 Posted : Thursday, January 3, 2013 10:35:24 PM(UTC)
Rank: NCrunch Developer

Groups: Administrators
Joined: 4/16/2011(UTC)
Posts: 7,123

Thanks: 957 times
Was thanked: 1286 time(s) in 1193 post(s)
This behaviour is due to the effect of NCrunch's instrumentation on garbage collection within the CLR. You can find some more details about this at: http://forum.ncrunch.net...ith-enabled-timing.aspx

To work around this problem, you can suppress code coverage for the full method above, or you can switch off performance analysis for the project that contains it.
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

YAF | YAF © 2003-2011, Yet Another Forum.NET
This page was generated in 0.048 seconds.
Trial NCrunch
Take NCrunch for a spin
Do your fingers a favour and supercharge your testing workflow
Free Download