Bug 12343 - Getting exception for Support4->Fragment->Retain Instance
Summary: Getting exception for Support4->Fragment->Retain Instance
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Samples ()
Version: 4.6.x
Hardware: PC Mac OS
: Normal normal
Target Milestone: 4.6.7
Assignee: Atsushi Eno
URL:
Depends on:
Blocks:
 
Reported: 2013-05-22 09:34 UTC by Nischal
Modified: 2013-06-21 10:55 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:
VERIFIED FIXED

Description Nischal 2013-05-22 09:34:09 UTC
Steps to reproduce:
1. Open Android sample Support4
2. Debug/run the application.
3. Go to Fragment
4. Click Retain Instance

Actual result: Application get crashed and throwing exception "Java.Lang.RuntimeException"

Expected Result: Application should not crashed after clicking on Fragment->Retain Instance.

Supplement info:
Android Logcat: https://gist.github.com/atin360/5527b99f3b1b83d3cd35
Application Output: https://gist.github.com/atin360/6d7809edb052d0d45281

Environment details:
X.S 4.0.7(build 3)
VS 2012/2010
Mono 3.0.11
Xamarin.Android 4.6.7-4

Device info:
Samsung S2
Version 2.3.3
Samsung Tab
Version 2.2

App info:
Monodroid Sample- master/Support4
Comment 1 Atin 2013-05-22 12:54:21 UTC
An update to the above issue, We have checked this with stable builds:
MFA 4.6.05003
VS 2010/2012
Windows 7 and 8
XS 4.0.7(build 3)
Mono 2.10.12

Regression status: NOT REGRESSION, Application get crashed and throwing exception after clicking on Fragment->retain Instance On MAC environment.

While on windows environment Application gets crashed as soon as launches on device.
Comment 2 Peter Collins 2013-05-22 14:08:53 UTC
I am able to reproduce this on a Motorola Droid Bionic v4.1.2 and an API 8 emulator.

Logcat:
https://gist.github.com/pjcollins/437b3e038fd83d5c88d1
Comment 3 Jonathan Pryor 2013-05-22 20:02:18 UTC
This is a porting bug:

The problem is FragmentRetainInstanceSupport.cs lines 96-99:

https://github.com/xamarin/monodroid-samples/blob/master/Support4/Support4/Activities/FragmentRetainInstanceSupport.cs#L96

	lock (myThread) {
		ready = true;
		myThread.Notify ();
	}

The issue is that Object.Notify() requires a _Java-side_ `synchronized` statement, which isn't possible to execute (doh!). The above code is thus invalid, as a C# `lock` IS NOT a Java `synchronized`. (Conceptually they do the same thing, but in the cross-VM case they're not the same.)

The result: failure:

> 05-22 14:07:53.334  6880  6917 I MonoDroid: UNHANDLED EXCEPTION: Java.Lang.IllegalMonitorStateException: Exception of type 'Java.Lang.IllegalMonitorStateException' was thrown.
> 05-22 14:07:53.334  6880  6917 I MonoDroid: at Android.Runtime.JNIEnv.CallVoidMethod (intptr,intptr) [0x00022] in /Users/builder/data/lanes/monodroid-lion-monodroid-4.6.6-branch/6d7480e9/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:356
> 05-22 14:07:53.334  6880  6917 I MonoDroid: at Java.Lang.Object.Wait () [0x0002d] in /Users/builder/data/lanes/monodroid-lion-monodroid-4.6.6-branch/6d7480e9/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Java.Lang.Object.cs:200
> 05-22 14:07:53.334  6880  6917 I MonoDroid: at Support4.FragmentRetainInstanceSupport/RetainedFragment/MyThread.Run () [0x00031] in /Users/petercollins/GitRepo/monodroid-samples/Support4/Support4/Activities/FragmentRetainInstanceSupport.cs:154
> 05-22 14:07:53.334  6880  6917 I MonoDroid: at Java.Lang.Thread.n_Run (intptr,intptr) [0x00008] in /Users/builder/data/lanes/monodroid-lion-monodroid-4.6.6-branch/6d7480e9/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Java.Lang.Thread.cs:962
> 05-22 14:07:53.334  6880  6917 I MonoDroid: at (wrapper dynamic-method) object.58e2a2aa-e710-4335-996e-dfe9bdfb6ea3 (intptr,intptr) <IL 0x00011, 0x0003b>
> 05-22 14:07:53.334  6880  6917 I MonoDroid: 
> 05-22 14:07:53.334  6880  6917 I MonoDroid:   --- End of managed exception stack trace ---
> 05-22 14:07:53.334  6880  6917 I MonoDroid: java.lang.IllegalMonitorStateException: object not locked by thread before wait()
> 05-22 14:07:53.334  6880  6917 I MonoDroid: 	at java.lang.Object.wait(Native Method)
> 05-22 14:07:53.334  6880  6917 I MonoDroid: 	at java.lang.Object.wait(Object.java:364)
> 05-22 14:07:53.334  6880  6917 I MonoDroid: 	at support4.FragmentRetainInstanceSupport_RetainedFragment_MyThread.n_run(Native Method)
> 05-22 14:07:53.334  6880  6917 I MonoDroid: 	at support4.FragmentRetainInstanceSupport_RetainedFragment_MyThread.run(FragmentRetainInstanceSupport_RetainedFragment_MyThread.java:91)

The sample needs to be rewritten to avoid using Java.Lang.Object.Notify()/etc. and stick to .NET-native thread primitives.
Comment 4 Atsushi Eno 2013-06-19 05:14:41 UTC
The crasher should go away now [master 6a9f5d4].

Though the sample never retains the instance state though - since it is almost complete port of Java sample, we likely need some more trick to actually retain instance. But I have no idea.
Comment 5 Atin 2013-06-21 10:55:24 UTC
Today, We have checked this issue with:

XS 4.0.9
VS 2012
MFA 4.6.9
Mono 2.10.12
App info: monodroid-samples db8bd7b275c60718176bfd6ada5c844f8be1fe66
API-8 Emulator

Device info:
Samsung s2 v 2.3.3
Samsung nexus 4.1.2

Now, Application is run successfully and not crashed after clicking on
Fragment->Retain Instance.

Hence marking this as Verified Fixed.