Bug 25845 - Unhandled exception finalizing ListViewAdapter on Android
Summary: Unhandled exception finalizing ListViewAdapter on Android
Status: VERIFIED FIXED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 1.3.0
Hardware: Other Other
: High major
Target Milestone: ---
Assignee: Eric Maupin
URL:
Depends on:
Blocks:
 
Reported: 2015-01-08 14:40 UTC by David Johnson
Modified: 2015-06-29 09:29 UTC (History)
16 users (show)

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


Attachments
DemoApplicationButton (4.41 MB, application/zip)
2015-01-15 13:42 UTC, Abhishek
Details


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:
VERIFIED FIXED

Description David Johnson 2015-01-08 14:40:03 UTC
I have not had time to attempt to create a project to demonstrate this bug, but I received this exception in my application after tapping a button on a ContentPage containing a ListView. The page is in a carousel, and the button causes another page to be added to the carousel and set to the CurrentPage.

[mono] 
[mono] Unhandled Exception:
[mono] System.ArgumentException: 'jobject' must not be IntPtr.Zero.
[mono] Parameter name: jobject
[mono]   at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:500 
[mono]   at Android.Widget.AdapterView.set_OnItemClickListener (IOnItemClickListener value) [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Widget.AdapterView.cs:920 
[mono]   at Xamarin.Forms.Platform.Android.ListViewAdapter.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
[mono]   at Java.Lang.Object.Finalize () [0x00056] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:61
Comment 1 Abhishek 2015-01-14 14:05:08 UTC
I have tried to reproduce this issue at my end but unable to reproduce the reported behavior.

I have add content Page Xaml  and in the .cs file add the following code 

public partial class DemoButton1 : ContentPage
	{
		public DemoButton1 ()
		{
			InitializeComponent ();

			var listView = new ListView
			{
				RowHeight = 40
			};
			listView.ItemsSource = new string []
			{
				"Buy pears",
				"Buy oranges",
				"Buy mangos",
				"Buy apples",
				"Buy bananas"
			};
			Content = new StackLayout
			{
				VerticalOptions = LayoutOptions.FillAndExpand,
				Children = { listView }
			};

			listView.ItemSelected += async (sender, e) => {
				await DisplayAlert("Tapped!", e.SelectedItem + " was tapped.", "OK");
			};
		}


	}

But it working fine for me. When I am tabbing the item it giving the popup.

If it is possible then could you please attach the test sample? so that I can reproduce the reported behavior at my end.

Build Info:
=== Xamarin Studio ===

Version 5.7.1 (build 2)
Installation UUID: 93e693b0-b53d-40f4-b29c-b61ff5cbe892
Runtime:
	Mono 3.12.0 ((detached/a813491)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000068

=== Apple Developer Tools ===

Xcode 6.1 (6604)
Build 6A1052d

=== Xamarin.Mac ===

Version: 1.11.3.0 (Business Edition)

=== Xamarin.Android ===

Version: 4.20.0.28 (Business Edition)
Android SDK: /Users/xamarin23/Desktop/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		4.0.3  (API level 15)
		4.1    (API level 16)
		4.2    (API level 17)
		4.3    (API level 18)
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin.iOS ===

Version: 8.6.0.52 (Business Edition)
Hash: 7c4c2c5
Branch: 
Build date: 2015-01-08 22:29:16-0500

=== Build Information ===

Release ID: 507010002
Git revision: 5a4e27c5dbd44e36bb1a0e2b63acf1e448fa2dbd
Build date: 2015-01-13 19:33:45-05
Xamarin addins: ee38284410f7625ad84602be0a5d0ade0ac779aa

=== Operating System ===

Mac OS X 10.10.0
Darwin 800BC.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 2 David Johnson 2015-01-14 17:50:31 UTC
I haven't been able to reproduce this reliably. However, your test does not match the scenario I described. First of all, you need to load this content page into a CarouselPage. Second, your ItemSelected handler needs to create another content page, add that to the carousel, and make that the current page.
Comment 3 Abhishek 2015-01-15 13:42:17 UTC
Created attachment 9368 [details]
DemoApplicationButton

Hi David,

I have create a sample test project "DemoApplicationButton" as suggest in Comment 2.

But I am still not able to reproduce the reported behavior. On clicking ItemSelected handler it creates another content page and which is added to the carousel, and make it as the current page.

Screencast: http://www.screencast.com/t/g5DJexXR70om

I am attaching my Test sample project. Please reviewed it and let me know if I have missed any step to create the test project?
Comment 4 David Johnson 2015-01-16 15:40:40 UTC
No, this is not correct. Your app should start with a CarouselPage that has a child ContentPage. That ContentPage should have a ListView and a button. When that button is clicked, another instance of that content page should be added to the carousel and be set to the CurrentPage.

As I've noted, this is very intermittent. I've only seen the crash three times out of over a hundred attempts. I believe it would be worthwhile to review the stack trace and the associated code. It looks pretty clear that the ListViewAdapter Dispose method is attempting to access a native control that is no longer available.
Comment 5 Jason Smith [MSFT] 2015-02-13 08:26:41 UTC
As far as I can tell this is now resolved in 1.3.4 (probably actually resolved in 1.3.3)

Please re-open if this is not the case.
Comment 6 Paul Roy 2015-03-19 13:02:32 UTC
Hi, 

I'm experiencing this bug on Xamarin Forms 1.4.0.6341.

My ListView is defined with a XAML file, it goes inside a ContentPage, itself inside a TabbedPage.

If you need more informations please don't hesitate.

I couldn't reproduce on debug but Xamarin.Insights logged this in production today.
Comment 7 David Johnson 2015-03-19 16:38:19 UTC
This bug is still happening in Xamarin Forms 1.4.0.6341:

java.lang.Throwable: System.ArgumentException: 'jobject' must not be IntPtr.Zero.
Parameter name: jobject
  at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0 
  at Android.Widget.AdapterView.set_OnItemClickListener (IOnItemClickListener value) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.ListViewAdapter.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
  at Java.Lang.Object.Finalize () [0x00000] in <filename unknown>:0 
	at mono.java.lang.RunnableImplementor.n_run(Native Method)
	at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:29)
	at android.os.Handler.handleCallback(Handler.java:733)
	at android.os.Handler.dispatchMessage(Handler.java:95)
	at android.os.Looper.loop(Looper.java:136)
	at android.app.ActivityThread.main(ActivityThread.java:5086)
	at java.lang.reflect.Method.invokeNative(Native Method)
	at java.lang.reflect.Method.invoke(Method.java:515)
	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
	at dalvik.system.NativeStart.main(Native Method)
Comment 8 David Johnson 2015-03-27 10:11:03 UTC
Is anybody looking into this? We are hitting this crashing bug frequently on 1.4.0.
Comment 9 Yurii 2015-03-28 19:12:42 UTC
I confirm this bug in XF 1.4.0.6341.

System.ArgumentException: 'jobject' must not be IntPtr.Zero.
 Parameter name: jobject
   at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00010] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:500 
   at Android.Widget.AdapterView.set_OnItemClickListener (IOnItemClickListener value) [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.21-series/49a04b96/source/monodroid/src/Mono.Android/platforms/android-21/src/generated/Android.Widget.AdapterView.cs:944 
   at Xamarin.Forms.Platform.Android.ListViewAdapter.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
   at Java.Lang.Object.Finalize
Comment 10 Thomas 2015-03-30 05:09:15 UTC
I can also confirm that the issue occurs in Xamarin.Forms 1.4.0.6341 when loading an external image as an ImageSource. The Android app only crashes when the images are loaded multiple times inside a ViewCell -> Image.
Comment 11 GeniusInPyjamas 2015-04-02 16:00:05 UTC
This error still happens in Xamarin.Forms 1.4.2.6353-pre2
Comment 12 David Johnson 2015-04-14 15:20:16 UTC
I still am getting this crash on Xamarin Forms 1.4.1.6349. This is a serious crashing bug that is preventing us from releasing our Android app. Why is it getting no attention?
Comment 13 mkoebi 2015-04-17 10:32:20 UTC
I am also still getting this error in 1.4.1.6349. Stack trace:
  at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00000] in <filename unknown>:0 
  at Android.Widget.AdapterView.set_OnItemClickListener (IOnItemClickListener value) [0x00000] in <filename unknown>:0 
  at Xamarin.Forms.Platform.Android.ListViewAdapter.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
  at Java.Lang.Object.Finalize () [0x00000] in <filename unknown>:0
Comment 14 Aaron 2015-04-27 04:11:33 UTC
Unfortunately this is still happening on 1.4.3.6358-pre2 and it is holding our Android app up from launch (which is getting quite urgent now).

Whilst we obviously don't expect a fix tomorrow, is anybody looking into this at the moment?
Comment 15 David Johnson 2015-05-12 19:30:13 UTC
Ok, it's been 4 months since I first reported this bug. My customers are complaining about crashes in our Android app. We've experienced 3 today due to this bug. Please provide an update on the status of this bug!
Comment 16 Jesus 2015-05-14 13:15:22 UTC
I confirm that the bug is still in Xamarin.Forms 1.4.2.6359
Comment 17 Jesus 2015-05-14 19:01:37 UTC
I just tried this and it seems to be working. It is not a solution, but I put this on my project and haven’t seen the error. In the ViewModel I created a public bool property IsVisible and use it OnPropertyChanged. This way, the ViewModel only send events to update the View when it is visible. Obviously, the trick is to make IsVisible = true at the right time.

public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
	if(IsVisible)
	{
		PropertyChangedEventHandler handler = PropertyChanged;
		if (handler != null)
		{
			handler(this,
				new PropertyChangedEventArgs(propertyName));
		}
	}
}
Comment 18 David Johnson 2015-05-19 16:53:41 UTC
That's interesting, Jesus. I already had put a hack into my code to ensure the ViewModel is not changed while the page is not visible due to an unrelated Xamarin Forms crashing bug on iOS. However, I still get this crash, and it's causing us a lot of distress. At this point having a crashing bug that's impacting my customers sit lingering for months has me really regretting the choice to use Xamarin Forms.
Comment 19 Jesus 2015-05-19 17:27:31 UTC
I am sorry for not reporting it David, but we were still getting the error. 
What has finally helped us was reusing the pages, instead of creating them before navigating to them. 

Since we are using XLabs Forms, for us this just involved switching ViewFactory.EnableCache to true. 
But if you are not using this library, you can see how they implemented it:
https://github.com/XLabs/Xamarin-Forms-Labs/blob/master/src/Forms/XLabs.Forms/Mvvm/ViewFactory.cs

Its just a dictionary where they keep references to the pages that are created.

Hope this works for you.
Comment 20 Eric Maupin 2015-05-20 17:44:04 UTC
I was not able to reliably reproduce this issue. However, based on the provided stack traces (thanks for those, everyone) I do believe I have tracked down the cause of it and fixed it. The prospective fix should go out with 1.4.3, so please try that when released and reopen the bug if it still occurs for you.
Comment 21 Rui Rasteiro 2015-05-22 05:48:56 UTC
Hi,

Is there a workaround we can implement while we wait for the fix?

Thanks.
Comment 22 Eric Maupin 2015-05-22 11:22:55 UTC
> Is there a workaround we can implement while we wait for the fix?

Unfortunately, no.
Comment 23 Brian 2015-06-15 11:43:39 UTC
Is this actually fixed?  When will it be released?  Its a major headache for an app I am working on.
Comment 24 Anonymous 2015-06-17 10:27:13 UTC
Bug is reported to be fixed in Xamarin.Forms 1.4.3-pre3.
Just wanted to update anyone waiting on this fix.
Comment 25 Brian 2015-06-17 10:52:04 UTC
I can confirmed its fixed in my project now.   Thank You.
Comment 26 Parmendra Kumar 2015-06-29 09:29:16 UTC
To verify this issue, I have tried to reproduce this issue with provided project sample link in comment #19 and instruction in comment #4 but not able to reproduce it.
So as per comment #25, it working fine at Brian end so I am closing this issue.
If anyone getting same issue please reopen this issue or filed a separate bug.

Thanks.