Fix bug in test regarding GC'd delegate instance. (#34901)
authorAaron Robinson <arobins@microsoft.com>
Mon, 13 Apr 2020 18:22:12 +0000 (11:22 -0700)
committerGitHub <noreply@github.com>
Mon, 13 Apr 2020 18:22:12 +0000 (11:22 -0700)
src/coreclr/tests/src/Interop/MarshalAPI/FunctionPointer/SingleMulticastTest.cs

index fb30bb9..99af2d2 100644 (file)
@@ -10,35 +10,35 @@ partial class FunctionPtr
     public delegate bool DelegateWithLong(long l); //Singlecast delegate
     public delegate void MultiDelegateWithLong(long l); //Multicast delegate
 
+    public static DelegateWithLong s_DelWithLongBool = new DelegateWithLong(MethodWithLongBool);
+    public static MultiDelegateWithLong s_MultidelWithLong = new MultiDelegateWithLong(MethodWithLong);
+
     public static void RunGetFcnPtrSingleMulticastTest()
     {
         Console.WriteLine($"Running {nameof(RunGetFcnPtrSingleMulticastTest)}...");
 
-        DelegateWithLong del = new DelegateWithLong(Method);
-        MultiDelegateWithLong multidel = new MultiDelegateWithLong(Method2);
-
         {
-            IntPtr fcnptr = Marshal.GetFunctionPointerForDelegate<DelegateWithLong>(del);
+            IntPtr fcnptr = Marshal.GetFunctionPointerForDelegate<DelegateWithLong>(s_DelWithLongBool);
             Assert.IsTrue(FunctionPointerNative.CheckFcnPtr(fcnptr));
         }
 
         {
-            IntPtr fcnptr = Marshal.GetFunctionPointerForDelegate<MultiDelegateWithLong>(multidel);
+            IntPtr fcnptr = Marshal.GetFunctionPointerForDelegate<MultiDelegateWithLong>(s_MultidelWithLong);
             FunctionPointerNative.CheckFcnPtr(fcnptr);
         }
+    }
 
-        bool Method(long l)
-        {
-            if (l != 999999999999)
-                return false;
-            else
-                return true;
-        }
+    private static bool MethodWithLongBool(long l)
+    {
+        if (l != 999999999999)
+            return false;
+        else
+            return true;
+    }
 
-        void Method2(long l)
-        {
-            if (l != 999999999999)
-                throw new Exception("Failed multicast call");
-        }
+    private static void MethodWithLong(long l)
+    {
+        if (l != 999999999999)
+            throw new Exception("Failed multicast call");
     }
 }