Fix profiler handle GC tests (#83968)
authorJakob Botsch Nielsen <Jakob.botsch.nielsen@gmail.com>
Thu, 30 Mar 2023 13:29:18 +0000 (15:29 +0200)
committerGitHub <noreply@github.com>
Thu, 30 Mar 2023 13:29:18 +0000 (15:29 +0200)
The JIT may extend the lifetime of these object references which can
keep them alive unexpectedly.

Fix #83962

src/tests/profiler/handles/handles.cs

index c54924a..a5c8289 100644 (file)
@@ -3,6 +3,7 @@
 
 using System;
 using System.Diagnostics;
+using System.Runtime.CompilerServices;
 using System.Threading;
 
 namespace Profiler.Tests
@@ -79,13 +80,7 @@ namespace Profiler.Tests
         {
             Objects objects = parameter as Objects;
 
-            AllocateInstances(objects);
-            Console.WriteLine($"weak = {objects._weak.Tag}");
-            objects._weak = null;
-            Console.WriteLine($"strong = {objects._strong.Tag}");
-            objects._strong = null;
-            Console.WriteLine($"weak = {objects._pinned.Tag}");
-            objects._pinned = null;
+            AllocateInstancesAndNullThemOut(objects);
 
             Console.WriteLine("Collection #1");
             GC.Collect(2);
@@ -113,6 +108,18 @@ namespace Profiler.Tests
             objects._afterPinned = "after pinned-" + Environment.ProcessId;
         }
 
+        [MethodImpl(MethodImplOptions.NoInlining)]
+        private static void AllocateInstancesAndNullThemOut(Objects objects)
+        {
+            AllocateInstances(objects);
+            Console.WriteLine($"weak = {objects._weak.Tag}");
+            objects._weak = null;
+            Console.WriteLine($"strong = {objects._strong.Tag}");
+            objects._strong = null;
+            Console.WriteLine($"weak = {objects._pinned.Tag}");
+            objects._pinned = null;
+        }
+
         public static int RunTest(String[] args) 
         {
             if(args.Length > 1)