1

The test code illustrated below fails to run if I run the test using the x64 "processor architecture" in visual studio.

If you create a test project (happens using both MSTest and NUnit) with the test below

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void GC_test()
    {
        var obj = new object();
        var weakRef = new WeakReference(obj);
        obj = null;
        GC.Collect();
        GC.WaitForPendingFinalizers();
        Assert.IsNull(obj);
        Assert.IsNull(weakRef.Target);
    }
}

It will fail if set the "Processor Architecture for AnyCPU Projects" to x64. Works fine running with Auto or x86, but fails with x64.

What is going on?

For context, I was trying to make sure some code didn't have any hard references when used that might prevent it from getting collected. My tests were continually failing, so I kept paring back until I had essentially what you see above.

Hamish
  • 579
  • 5
  • 20
  • 1
    `GC.Collect(2); GC.WaitForPendingFinalizers(); GC.Collect(2);` from **outside** the function declaring the object – Charlieface Feb 12 '21 at 09:12

0 Answers0