Bug 6531 - generator: field property setters leak JNI local references
Summary: generator: field property setters leak JNI local references
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Tools and Addins ()
Version: 4.2.x
Hardware: PC Mac OS
: Highest blocker
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-08-14 15:05 UTC by Jonathan Pryor
Modified: 2012-08-14 21:24 UTC (History)
1 user (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Jonathan Pryor 2012-08-14 15:05:53 UTC
Context: http://lists.ximian.com/pipermail/monodroid/2012-August/011797.html

Create a default Project within MonoDevelop/Visual Studio, and replace the default Activity with:

  using System;
  using Android.App;
  using Android.Widget;
  using Android.OS;

  namespace Scratch.FieldSetLrefOverflow
  {
    [Activity (Label = "Scratch.FieldSetLrefOverflow", MainLauncher = true)]
      public class Activity1 : Activity
    {
      protected override void OnCreate (Bundle bundle)
      {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        Button button = FindViewById<Button> (Resource.Id.myButton);

        button.Click += delegate {
          int c = 0;
          var r = new Messenger (new Handler (x => {}));
          var m = new Message ();
          while (true) {
            c++;
            if ((c % 10) == 0)
              Console.WriteLine ("lrefs? {0}", c);
            m.ReplyTo = r;
          }
        };
      }
    }
  }

Run the app, then click the button once loaded.

Expected behavior: no app crash, and `adb logcat` is spammed with ever increasing numbers (certainly above 510).

Actual behavior: *BOOM*

> I/mono-stdout(15295): lrefs? 500
> E/dalvikvm(15295): JNI ERROR (app bug): local reference table overflow (max=512)
> W/dalvikvm(15295): JNI local reference table (0x58136d68) dump:
> W/dalvikvm(15295):   Last 10 entries (of 512):
> W/dalvikvm(15295):       511: 0x4180df08 android.os.Messenger
> W/dalvikvm(15295):       510: 0x4180df08 android.os.Messenger
> W/dalvikvm(15295):       509: 0x4180df08 android.os.Messenger
> W/dalvikvm(15295):       508: 0x4180df08 android.os.Messenger
> W/dalvikvm(15295):       507: 0x4180df08 android.os.Messenger
> W/dalvikvm(15295):       506: 0x4180df08 android.os.Messenger
> W/dalvikvm(15295):       505: 0x4180df08 android.os.Messenger
> W/dalvikvm(15295):       504: 0x4180df08 android.os.Messenger
> W/dalvikvm(15295):       503: 0x4180df08 android.os.Messenger
> W/dalvikvm(15295):       502: 0x4180df08 android.os.Messenger
> W/dalvikvm(15295):   Summary:
> W/dalvikvm(15295):         3 of java.lang.Class (3 unique instances)
> W/dalvikvm(15295):         2 of java.lang.String (2 unique instances)
> W/dalvikvm(15295):         1 of java.lang.String[] (2 elements)
> W/dalvikvm(15295):         1 of android.widget.Button
> W/dalvikvm(15295):         1 of mono.android.view.View_OnClickListenerImplementor
> W/dalvikvm(15295):       504 of android.os.Messenger (1 unique instances)
> E/dalvikvm(15295): Failed adding to JNI local ref table (has 512 entries)
> I/dalvikvm(15295): "main" prio=5 tid=1 RUNNABLE
> I/dalvikvm(15295):   | group="main" sCount=0 dsCount=0 obj=0x40d61568 self=0x40c7bab0
> I/dalvikvm(15295):   | sysTid=15295 nice=0 sched=0/0 cgrp=apps handle=1074968112
> I/dalvikvm(15295):   | schedstat=( 0 0 0 ) utm=77 stm=18 core=1
> I/dalvikvm(15295):   at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
> I/dalvikvm(15295):   at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
> I/dalvikvm(15295):   at android.view.View.performClick(View.java:4084)
> I/dalvikvm(15295):   at android.view.View$PerformClick.run(View.java:16966)
> I/dalvikvm(15295):   at android.os.Handler.handleCallback(Handler.java:615)
> I/dalvikvm(15295):   at android.os.Handler.dispatchMessage(Handler.java:92)
> I/dalvikvm(15295):   at android.os.Looper.loop(Looper.java:137)
> I/dalvikvm(15295):   at android.app.ActivityThread.main(ActivityThread.java:4745)
> I/dalvikvm(15295):   at java.lang.reflect.Method.invokeNative(Native Method)
> I/dalvikvm(15295):   at java.lang.reflect.Method.invoke(Method.java:511)
> I/dalvikvm(15295):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
> I/dalvikvm(15295):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
> I/dalvikvm(15295):   at dalvik.system.NativeStart.main(Native Method)
> I/dalvikvm(15295): 
> E/dalvikvm(15295): VM aborting
Comment 1 Jonathan Pryor 2012-08-14 15:07:30 UTC
The problem is that `generator` generates the following code for the Message.ReplyTo field binding:

	[Register ("replyTo")]
	public Android.OS.Messenger ReplyTo {
		get { /* ... */ }
		set {
			if (replyTo_jfieldId == IntPtr.Zero)
				replyTo_jfieldId = JNIEnv.GetFieldID (class_ref, "replyTo", "Landroid/os/Messenger;");
			JNIEnv.SetField (Handle, replyTo_jfieldId, JNIEnv.ToLocalJniHandle (value));
		}
	}

The issue is that JNIEnv.ToLocalJniHandle() allocates a local reference, but it isn't freed. What it should instead be is:

	[Register ("replyTo")]
	public Android.OS.Messenger ReplyTo {
		get { /* ... */ }
		set {
			if (replyTo_jfieldId == IntPtr.Zero)
				replyTo_jfieldId = JNIEnv.GetFieldID (class_ref, "replyTo", "Landroid/os/Messenger;");
			IntPtr __value = JNIEnv.ToLocalJniHandle (value);
			JNIEnv.SetField (Handle, replyTo_jfieldId, __value);
			JNIEnv.DeleteLocalRef (__value);
		}
	}
Comment 2 Jonathan Pryor 2012-08-14 21:24:59 UTC
Fixed in e67794c0.