diff --git a/src/tests/GC/API/WeakReference/IsAlive.cs b/src/tests/GC/API/WeakReference/IsAlive.cs index db3a01040d4cf3..2aa125cf3a8eac 100644 --- a/src/tests/GC/API/WeakReference/IsAlive.cs +++ b/src/tests/GC/API/WeakReference/IsAlive.cs @@ -1,7 +1,7 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -// Tests WeakReference.IsAlive : IsAlive=true if GC has not occurred on the object +// Tests WeakReference.IsAlive : IsAlive=true if GC has not occurred on the object using System; @@ -11,35 +11,40 @@ public class Test_IsAlive { public static int[] array; - + public static WeakReference weak; + [MethodImplAttribute(MethodImplOptions.NoInlining)] public static void CreateArray() { array = new int[50]; + // Create the weak reference inside of CreateArray to prevent a dangling 'array' reference + // from surviving inside of TestEntryPoint. + weak = new WeakReference(array); } - + [MethodImplAttribute(MethodImplOptions.NoInlining)] public static void DestroyArray() { array = null; } - + [Fact] public static int TestEntryPoint() { CreateArray(); - WeakReference weak = new WeakReference(array); - bool ans1 = weak.IsAlive; Console.WriteLine(ans1); - - if(ans1==false) { // GC.Collect() has already occurred..under GCStress - Console.WriteLine("Test for WeakReference.IsAlive passed!"); - return 100; + if (ans1 != true) + { + // This should be impossible; it would indicate that either the array was collected while reachable from + // our static field, or that the WeakReference failed to track the array even though it's still alive. + Console.WriteLine("Test for WeakReference.IsAlive failed!"); + return 2; } - //else, do an expicit collect. + // Release our strong reference (via static field) so that the collector will no longer see array as reachable. DestroyArray(); + // Perform a blocking full collection which will hopefully collect the array. GC.Collect(); - + bool ans2 = weak.IsAlive; Console.WriteLine(ans2); diff --git a/src/tests/issues.targets b/src/tests/issues.targets index 5d91deeba59eb2..954936a1c3bf26 100644 --- a/src/tests/issues.targets +++ b/src/tests/issues.targets @@ -576,9 +576,6 @@ https://github.com/dotnet/runtime/issues/109311 - - https://github.com/dotnet/runtime/issues/109312 - https://github.com/dotnet/runtime/issues/109313