Fix WeakReference tests depending on JIT not extending lifetimes
authorRuss Keldorph <Russ.Keldorph@microsoft.com>
Thu, 9 Jun 2016 17:36:44 +0000 (10:36 -0700)
committerRuss Keldorph <Russ.Keldorph@microsoft.com>
Thu, 9 Jun 2016 17:41:08 +0000 (10:41 -0700)
Target and IsAlive_neg are failing in some cases when JIT optimization is
disabled because the JIT (legally) generates code that holds a reference
to an object longer than expected.  Instead of forcing optimization on for
these tests, I'm using the pattern of moving the problematic object
reference to a non-inlined helper method.

Fixes dotnet/coreclr#5555

Commit migrated from https://github.com/dotnet/coreclr/commit/e784d4d1c82adea07d6e5e118b1ae6624c7e0d8b

src/coreclr/tests/src/GC/API/WeakReference/IsAlive_neg.cs
src/coreclr/tests/src/GC/API/WeakReference/Target.cs

index c1b9ba8..d229f59 100644 (file)
@@ -16,6 +16,12 @@ public class Test {
     public static void CreateArray() {
         array = new int[50];
     }
+
+    [MethodImplAttribute(MethodImplOptions.NoInlining)]
+    public static WeakReference CreateArrayWeakReference()
+    {
+        return new WeakReference(array);
+    }
     
     [MethodImplAttribute(MethodImplOptions.NoInlining)]
     public static void DestroyArray() {
@@ -25,7 +31,7 @@ public class Test {
     public static int Main() {
         CreateArray();
 
-        WeakReference weak = new WeakReference(array); // array has ONLY a weakreference
+        WeakReference weak = CreateArrayWeakReference(); // array has ONLY a weakreference
 
         // ensuring that GC happens even with /debug mode
         DestroyArray();
index 64d2a2b..91eb161 100644 (file)
@@ -32,6 +32,12 @@ public class Test
     }
     
     [MethodImplAttribute(MethodImplOptions.NoInlining)]
+    public static WeakReference CreateArrayWeakReference()
+    {
+        return new WeakReference(array);
+    }
+
+    [MethodImplAttribute(MethodImplOptions.NoInlining)]
     public static void DestroyArrays() 
     {
         array = null;
@@ -41,7 +47,7 @@ public class Test
     public bool GetTargetTest()
     {
         CreateArrays();
-        WeakReference weakarray = new WeakReference(array); // array has only weak reference
+        WeakReference weakarray = CreateArrayWeakReference(); // array has only weak reference
 
         // obj has both strong and weak ref and so should not get collected