Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 2de3a3c

Browse files
jkotasstephentoub
authored andcommitted
Keep marshalled delegate alive while it is used (#28092)
Fixes #28063
1 parent d379aba commit 2de3a3c

File tree

1 file changed

+18
-4
lines changed
  • src/System.Reflection.Emit.ILGeneration/tests/ILGenerator

1 file changed

+18
-4
lines changed

src/System.Reflection.Emit.ILGeneration/tests/ILGenerator/Emit4Tests.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@ public void TestEmitCalliBlittable()
3131
il.Emit(OpCodes.Ret);
3232

3333
Type dynamicType = typeBuilder.CreateType();
34-
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(new Int32SumStdCall(Int32Sum));
34+
35+
var del = new Int32SumStdCall(Int32Sum);
36+
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(del);
3537

3638
object resultValue = dynamicType
3739
.GetMethod("F", BindingFlags.Public | BindingFlags.Static)
3840
.Invoke(null, new object[] { funcPtr, a, b });
3941

42+
GC.KeepAlive(del);
43+
4044
Assert.IsType(returnType, resultValue);
4145
Assert.Equal(result, resultValue);
4246
}
@@ -57,11 +61,14 @@ public void TestDynamicMethodEmitCalliBlittable()
5761
il.EmitCalli(OpCodes.Calli, CallingConvention.StdCall, returnType, new Type[] { typeof(int), typeof(int) });
5862
il.Emit(OpCodes.Ret);
5963

60-
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(new Int32SumStdCall(Int32Sum));
64+
var del = new Int32SumStdCall(Int32Sum);
65+
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(del);
6166

6267
object resultValue = dynamicMethod
6368
.Invoke(null, new object[] { funcPtr, a, b });
6469

70+
GC.KeepAlive(del);
71+
6572
Assert.IsType(returnType, resultValue);
6673
Assert.Equal(result, resultValue);
6774
}
@@ -86,12 +93,16 @@ public void TestEmitCalliNonBlittable()
8693
il.Emit(OpCodes.Ret);
8794

8895
Type dynamicType = typeBuilder.CreateType();
89-
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(new StringReverseCdecl(StringReverse));
96+
97+
var del = new StringReverseCdecl(StringReverse);
98+
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(del);
9099

91100
object resultValue = dynamicType
92101
.GetMethod("F", BindingFlags.Public | BindingFlags.Static)
93102
.Invoke(null, new object[] { funcPtr, input });
94103

104+
GC.KeepAlive(del);
105+
95106
Assert.IsType(returnType, resultValue);
96107
Assert.Equal(result, resultValue);
97108
}
@@ -111,11 +122,14 @@ public void TestDynamicMethodEmitCalliNonBlittable()
111122
il.EmitCalli(OpCodes.Calli, CallingConvention.Cdecl, returnType, new Type[] { typeof(string) });
112123
il.Emit(OpCodes.Ret);
113124

114-
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(new StringReverseCdecl(StringReverse));
125+
var del = new StringReverseCdecl(StringReverse);
126+
IntPtr funcPtr = Marshal.GetFunctionPointerForDelegate(del);
115127

116128
object resultValue = dynamicMethod
117129
.Invoke(null, new object[] { funcPtr, input });
118130

131+
GC.KeepAlive(del);
132+
119133
Assert.IsType(returnType, resultValue);
120134
Assert.Equal(result, resultValue);
121135
}

0 commit comments

Comments
 (0)