From: Russ Keldorph Date: Thu, 9 Jun 2016 17:36:44 +0000 (-0700) Subject: Fix WeakReference tests depending on JIT not extending lifetimes X-Git-Tag: submit/tizen/20210909.063632~11030^2~10185^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=cb536db5a6c16d751e03dd84c1ec6f49012305e4;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Fix WeakReference tests depending on JIT not extending lifetimes 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 --- diff --git a/src/coreclr/tests/src/GC/API/WeakReference/IsAlive_neg.cs b/src/coreclr/tests/src/GC/API/WeakReference/IsAlive_neg.cs index c1b9ba8..d229f59 100644 --- a/src/coreclr/tests/src/GC/API/WeakReference/IsAlive_neg.cs +++ b/src/coreclr/tests/src/GC/API/WeakReference/IsAlive_neg.cs @@ -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(); diff --git a/src/coreclr/tests/src/GC/API/WeakReference/Target.cs b/src/coreclr/tests/src/GC/API/WeakReference/Target.cs index 64d2a2b..91eb161 100644 --- a/src/coreclr/tests/src/GC/API/WeakReference/Target.cs +++ b/src/coreclr/tests/src/GC/API/WeakReference/Target.cs @@ -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