Skip to content

Commit 143a0f7

Browse files
committed
[generator] Use GC.KeepAlive for reference type method parameters.
1 parent a807961 commit 143a0f7

32 files changed

+81
-0
lines changed

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteClass.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public partial class MyClass {
162162
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
163163
} finally {
164164
JNIEnv.DeleteLocalRef (native_value);
165+
global::System.GC.KeepAlive (value);
165166
}
166167
}
167168
}
@@ -260,6 +261,7 @@ public partial class MyClass {
260261
return __rm;
261262
} finally {
262263
JNIEnv.DeleteLocalRef (native_key);
264+
global::System.GC.KeepAlive (key);
263265
}
264266
}
265267

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteClassMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public virtual unsafe int GetCountForKey (string key)
2929
return __rm;
3030
} finally {
3131
JNIEnv.DeleteLocalRef (native_key);
32+
global::System.GC.KeepAlive (key);
3233
}
3334
}
3435

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteClassProperties.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public virtual unsafe string Key {
109109
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
110110
} finally {
111111
JNIEnv.DeleteLocalRef (native_value);
112+
global::System.GC.KeepAlive (value);
112113
}
113114
}
114115
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteKotlinUnsignedArrayTypeMethodsClass.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public partial class MyClass {
2727
JNIEnv.CopyArray (native_value, value);
2828
JNIEnv.DeleteLocalRef (native_value);
2929
}
30+
global::System.GC.KeepAlive (value);
3031
}
3132
}
3233

@@ -46,6 +47,7 @@ public partial class MyClass {
4647
JNIEnv.CopyArray (native_value, value);
4748
JNIEnv.DeleteLocalRef (native_value);
4849
}
50+
global::System.GC.KeepAlive (value);
4951
}
5052
}
5153

@@ -65,6 +67,7 @@ public partial class MyClass {
6567
JNIEnv.CopyArray (native_value, value);
6668
JNIEnv.DeleteLocalRef (native_value);
6769
}
70+
global::System.GC.KeepAlive (value);
6871
}
6972
}
7073

@@ -84,6 +87,7 @@ public partial class MyClass {
8487
JNIEnv.CopyArray (native_value, value);
8588
JNIEnv.DeleteLocalRef (native_value);
8689
}
90+
global::System.GC.KeepAlive (value);
8791
}
8892
}
8993

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteKotlinUnsignedArrayTypePropertiesClass.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public partial class MyClass {
3636
JNIEnv.CopyArray (native_value, value);
3737
JNIEnv.DeleteLocalRef (native_value);
3838
}
39+
global::System.GC.KeepAlive (value);
3940
}
4041
}
4142
}
@@ -65,6 +66,7 @@ public partial class MyClass {
6566
JNIEnv.CopyArray (native_value, value);
6667
JNIEnv.DeleteLocalRef (native_value);
6768
}
69+
global::System.GC.KeepAlive (value);
6870
}
6971
}
7072
}
@@ -94,6 +96,7 @@ public partial class MyClass {
9496
JNIEnv.CopyArray (native_value, value);
9597
JNIEnv.DeleteLocalRef (native_value);
9698
}
99+
global::System.GC.KeepAlive (value);
97100
}
98101
}
99102
}
@@ -123,6 +126,7 @@ public partial class MyClass {
123126
JNIEnv.CopyArray (native_value, value);
124127
JNIEnv.DeleteLocalRef (native_value);
125128
}
129+
global::System.GC.KeepAlive (value);
126130
}
127131
}
128132
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/JavaInterop1/WriteKotlinUnsignedTypePropertiesClass.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public partial class MyClass {
103103
__args [0] = new JniArgumentValue (value);
104104
_members.InstanceMethods.InvokeAbstractVoidMethod (__id, this, __args);
105105
} finally {
106+
global::System.GC.KeepAlive (value);
106107
}
107108
}
108109
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteClass.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public partial class MyClass {
162162
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
163163
} finally {
164164
JNIEnv.DeleteLocalRef (native_value);
165+
global::System.GC.KeepAlive (value);
165166
}
166167
}
167168
}
@@ -260,6 +261,7 @@ public partial class MyClass {
260261
return __rm;
261262
} finally {
262263
JNIEnv.DeleteLocalRef (native_key);
264+
global::System.GC.KeepAlive (key);
263265
}
264266
}
265267

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteClassMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public virtual unsafe int GetCountForKey (string? key)
2929
return __rm;
3030
} finally {
3131
JNIEnv.DeleteLocalRef (native_key);
32+
global::System.GC.KeepAlive (key);
3233
}
3334
}
3435

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1-NRT/WriteClassProperties.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public virtual unsafe string? Key {
109109
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
110110
} finally {
111111
JNIEnv.DeleteLocalRef (native_value);
112+
global::System.GC.KeepAlive (value);
112113
}
113114
}
114115
}

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteClass.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ public partial class MyClass {
162162
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
163163
} finally {
164164
JNIEnv.DeleteLocalRef (native_value);
165+
global::System.GC.KeepAlive (value);
165166
}
166167
}
167168
}
@@ -260,6 +261,7 @@ public partial class MyClass {
260261
return __rm;
261262
} finally {
262263
JNIEnv.DeleteLocalRef (native_key);
264+
global::System.GC.KeepAlive (key);
263265
}
264266
}
265267

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteClassMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public virtual unsafe int GetCountForKey (string key)
2929
return __rm;
3030
} finally {
3131
JNIEnv.DeleteLocalRef (native_key);
32+
global::System.GC.KeepAlive (key);
3233
}
3334
}
3435

tests/generator-Tests/Unit-Tests/CodeGeneratorExpectedResults/XAJavaInterop1/WriteClassProperties.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public virtual unsafe string Key {
109109
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
110110
} finally {
111111
JNIEnv.DeleteLocalRef (native_value);
112+
global::System.GC.KeepAlive (value);
112113
}
113114
}
114115
}

tests/generator-Tests/expected.ji/Adapters/Xamarin.Test.AbsSpinner.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ static void n_SetAdapter_Lxamarin_test_SpinnerAdapter_ (IntPtr jnienv, IntPtr na
8383
__args [0] = new JniArgumentValue ((value == null) ? IntPtr.Zero : ((global::Java.Lang.Object) value).Handle);
8484
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
8585
} finally {
86+
global::System.GC.KeepAlive (value);
8687
}
8788
}
8889
}
@@ -126,6 +127,7 @@ public AbsSpinnerInvoker (IntPtr handle, JniHandleOwnership transfer) : base (ha
126127
_members.InstanceMethods.InvokeAbstractVoidMethod (__id, this, __args);
127128
} finally {
128129
JNIEnv.DeleteLocalRef (native_value);
130+
global::System.GC.KeepAlive (value);
129131
}
130132
}
131133
}

tests/generator-Tests/expected.ji/Adapters/Xamarin.Test.AdapterView.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public AdapterViewInvoker (IntPtr handle, JniHandleOwnership transfer) : base (h
110110
_members.InstanceMethods.InvokeAbstractVoidMethod (__id, this, __args);
111111
} finally {
112112
JNIEnv.DeleteLocalRef (native_value);
113+
global::System.GC.KeepAlive (value);
113114
}
114115
}
115116
}

tests/generator-Tests/expected.ji/CSharpKeywords/Xamarin.Test.CSharpKeywords.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public static unsafe string UseThis (string this_)
7373
return JNIEnv.GetString (__rm.Handle, JniHandleOwnership.TransferLocalRef);
7474
} finally {
7575
JNIEnv.DeleteLocalRef (native_this);
76+
global::System.GC.KeepAlive (this_);
7677
}
7778
}
7879

tests/generator-Tests/expected.ji/Core_Jar2Xml/Android.Text.SpannableString.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ static int n_GetSpanFlags_Ljava_lang_Object_ (IntPtr jnienv, IntPtr native__this
102102
var __rm = _members.InstanceMethods.InvokeVirtualInt32Method (__id, this, __args);
103103
return (global::Android.Text.SpanTypes) __rm;
104104
} finally {
105+
global::System.GC.KeepAlive (what);
105106
}
106107
}
107108

tests/generator-Tests/expected.ji/Core_Jar2Xml/Android.Text.SpannableStringInternal.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static int n_GetSpanFlags_Ljava_lang_Object_ (IntPtr jnienv, IntPtr native__this
6060
var __rm = _members.InstanceMethods.InvokeVirtualInt32Method (__id, this, __args);
6161
return (global::Android.Text.SpanTypes) __rm;
6262
} finally {
63+
global::System.GC.KeepAlive (p0);
6364
}
6465
}
6566

tests/generator-Tests/expected.ji/Core_Jar2Xml/Android.Views.View.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ public virtual unsafe void SetOnClickListener (global::Android.Views.View.IOnCli
176176
__args [0] = new JniArgumentValue ((l == null) ? IntPtr.Zero : ((global::Java.Lang.Object) l).Handle);
177177
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
178178
} finally {
179+
global::System.GC.KeepAlive (l);
179180
}
180181
}
181182

@@ -206,6 +207,7 @@ public virtual unsafe void SetOn123Listener (global::Android.Views.View.IOnClick
206207
__args [0] = new JniArgumentValue ((l == null) ? IntPtr.Zero : ((global::Java.Lang.Object) l).Handle);
207208
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
208209
} finally {
210+
global::System.GC.KeepAlive (l);
209211
}
210212
}
211213

@@ -238,6 +240,7 @@ public virtual unsafe void AddTouchables (global::System.Collections.Generic.ILi
238240
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
239241
} finally {
240242
JNIEnv.DeleteLocalRef (native_views);
243+
global::System.GC.KeepAlive (views);
241244
}
242245
}
243246

tests/generator-Tests/expected.ji/GenericArguments/Com.Google.Android.Exoplayer.Drm.FrameworkMediaDrm.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public unsafe void SetOnEventListener (global::Com.Google.Android.Exoplayer.Drm.
5858
__args [0] = new JniArgumentValue ((p0 == null) ? IntPtr.Zero : ((global::Java.Lang.Object) p0).Handle);
5959
_members.InstanceMethods.InvokeAbstractVoidMethod (__id, this, __args);
6060
} finally {
61+
global::System.GC.KeepAlive (p0);
6162
}
6263
}
6364

tests/generator-Tests/expected.ji/NormalMethods/Xamarin.Test.SomeObject.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ static int n_Handle_Ljava_lang_Object_Ljava_lang_Throwable_ (IntPtr jnienv, IntP
109109
var __rm = _members.InstanceMethods.InvokeVirtualInt32Method (__id, this, __args);
110110
return __rm;
111111
} finally {
112+
global::System.GC.KeepAlive (o);
113+
global::System.GC.KeepAlive (t);
112114
}
113115
}
114116

@@ -255,6 +257,8 @@ public virtual unsafe void VoidMethodWithParams (string astring, int anint, glob
255257
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
256258
} finally {
257259
JNIEnv.DeleteLocalRef (native_astring);
260+
global::System.GC.KeepAlive (astring);
261+
global::System.GC.KeepAlive (anObject);
258262
}
259263
}
260264

@@ -318,6 +322,7 @@ public virtual unsafe void ArrayListTest (global::System.Collections.Generic.ILi
318322
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
319323
} finally {
320324
JNIEnv.DeleteLocalRef (native_p0);
325+
global::System.GC.KeepAlive (p0);
321326
}
322327
}
323328

tests/generator-Tests/expected.ji/NormalProperties/Xamarin.Test.SomeObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ public override unsafe int SomeInteger {
210210
__args [0] = new JniArgumentValue ((value == null) ? IntPtr.Zero : ((global::Java.Lang.Object) value).Handle);
211211
_members.InstanceMethods.InvokeAbstractVoidMethod (__id, this, __args);
212212
} finally {
213+
global::System.GC.KeepAlive (value);
213214
}
214215
}
215216
}
@@ -236,6 +237,7 @@ public override unsafe string SomeString {
236237
_members.InstanceMethods.InvokeAbstractVoidMethod (__id, this, __args);
237238
} finally {
238239
JNIEnv.DeleteLocalRef (native_value);
240+
global::System.GC.KeepAlive (value);
239241
}
240242
}
241243
}

tests/generator-Tests/expected.ji/ParameterXPath/Xamarin.Test.A.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public virtual unsafe void SetA (global::Java.Lang.Object adapter)
6060
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
6161
} finally {
6262
JNIEnv.DeleteLocalRef (native_adapter);
63+
global::System.GC.KeepAlive (adapter);
6364
}
6465
}
6566

@@ -92,6 +93,7 @@ public virtual unsafe void ListTest (global::System.Collections.Generic.IList<gl
9293
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
9394
} finally {
9495
JNIEnv.DeleteLocalRef (native_p0);
96+
global::System.GC.KeepAlive (p0);
9597
}
9698
}
9799

tests/generator-Tests/expected.ji/StaticProperties/Xamarin.Test.SomeObject.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public static unsafe string SomeString {
7676
_members.StaticMethods.InvokeVoidMethod (__id, __args);
7777
} finally {
7878
JNIEnv.DeleteLocalRef (native_value);
79+
global::System.GC.KeepAlive (value);
7980
}
8081
}
8182
}
@@ -102,6 +103,7 @@ public static unsafe void SetSomeObject (global::Java.Lang.Object newvalue)
102103
__args [0] = new JniArgumentValue ((newvalue == null) ? IntPtr.Zero : ((global::Java.Lang.Object) newvalue).Handle);
103104
_members.StaticMethods.InvokeVoidMethod (__id, __args);
104105
} finally {
106+
global::System.GC.KeepAlive (newvalue);
105107
}
106108
}
107109

tests/generator-Tests/expected.ji/Streams/Java.IO.InputStream.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public virtual unsafe int Read (byte[] buffer)
216216
JNIEnv.CopyArray (native_buffer, buffer);
217217
JNIEnv.DeleteLocalRef (native_buffer);
218218
}
219+
global::System.GC.KeepAlive (buffer);
219220
}
220221
}
221222

@@ -257,6 +258,7 @@ public virtual unsafe int Read (byte[] buffer, int byteOffset, int byteCount)
257258
JNIEnv.CopyArray (native_buffer, buffer);
258259
JNIEnv.DeleteLocalRef (native_buffer);
259260
}
261+
global::System.GC.KeepAlive (buffer);
260262
}
261263
}
262264

tests/generator-Tests/expected.ji/Streams/Java.IO.OutputStream.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public virtual unsafe void Write (byte[] buffer)
136136
JNIEnv.CopyArray (native_buffer, buffer);
137137
JNIEnv.DeleteLocalRef (native_buffer);
138138
}
139+
global::System.GC.KeepAlive (buffer);
139140
}
140141
}
141142

@@ -175,6 +176,7 @@ public virtual unsafe void Write (byte[] buffer, int offset, int count)
175176
JNIEnv.CopyArray (native_buffer, buffer);
176177
JNIEnv.DeleteLocalRef (native_buffer);
177178
}
179+
global::System.GC.KeepAlive (buffer);
178180
}
179181
}
180182

tests/generator-Tests/expected.ji/TestInterface/Test.ME.GenericImplementation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public virtual unsafe void SetObject (byte[] value)
8282
JNIEnv.CopyArray (native_value, value);
8383
JNIEnv.DeleteLocalRef (native_value);
8484
}
85+
global::System.GC.KeepAlive (value);
8586
}
8687
}
8788

tests/generator-Tests/expected.ji/TestInterface/Test.ME.GenericObjectPropertyImplementation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ static void n_SetObject_Ljava_lang_Object_ (IntPtr jnienv, IntPtr native__this,
101101
__args [0] = new JniArgumentValue ((value == null) ? IntPtr.Zero : ((global::Java.Lang.Object) value).Handle);
102102
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
103103
} finally {
104+
global::System.GC.KeepAlive (value);
104105
}
105106
}
106107
}

tests/generator-Tests/expected.ji/TestInterface/Test.ME.GenericStringImplementation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public virtual unsafe void SetObject (string[] value)
8282
JNIEnv.CopyArray (native_value, value);
8383
JNIEnv.DeleteLocalRef (native_value);
8484
}
85+
global::System.GC.KeepAlive (value);
8586
}
8687
}
8788

tests/generator-Tests/expected.ji/TestInterface/Test.ME.GenericStringPropertyImplementation.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public virtual unsafe string Object {
103103
_members.InstanceMethods.InvokeVirtualVoidMethod (__id, this, __args);
104104
} finally {
105105
JNIEnv.DeleteLocalRef (native_value);
106+
global::System.GC.KeepAlive (value);
106107
}
107108
}
108109
}

0 commit comments

Comments
 (0)