Xamarin-android: ObjectDisposedException caused by GC

Created on 8 Aug 2018  路  14Comments  路  Source: xamarin/xamarin-android

Steps to Reproduce

  1. Clone Xamarin.Forms: https://github.com/xamarin/Xamarin.Forms.git
  2. Switch to the handle-testing branch: https://github.com/xamarin/Xamarin.Forms/compare/handle-testing
  3. Build Xamarin.Forms.sln, you can open in the IDE and build or nuget restore Xamarin.Forms.sln + msbuild Xamarin.Forms.sln _should_ work
  4. Open .Xamarin.Forms.Android.sln in an IDE and set Xamarin.Forms.ControlGallery.Android as the startup project
  5. Debug the app

After a few screens fly by on the device, you will get a crash on this line: https://github.com/xamarin/Xamarin.Forms/compare/handle-testing#diff-cfe75ccaa32b145fd9a96b49cb931d1dR72

image

android.runtime.JavaProxyThrowable: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Android.Graphics.Bitmap'.
  at Java.Interop.JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self) [0x00029] in <e8c0e16a54534fa885244f0ad837c79e>:0 
  at Java.Interop.JniPeerMembers+JniInstanceMethods.InvokeNonvirtualInt32Method (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters) [0x00000] in <e8c0e16a54534fa885244f0ad837c79e>:0 
  at Android.Graphics.Bitmap.get_Height () [0x0000a] in <848bbd7c681a4975918c72f17d2f5144>:0 
  at Xamarin.Forms.Platform.Android.ButtonDrawable.Draw (Android.Graphics.Canvas canvas) [0x00021] in D:\agent_work\1\s\Xamarin.Forms.Platform.Android\Renderers\ButtonDrawable.cs:55 
  at Android.Graphics.Drawables.Drawable.n_Draw_Landroid_graphics_Canvas_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_canvas) [0x0000f] in <848bbd7c681a4975918c72f17d2f5144>:0 

While the debugger has paused, this.Handle has recently changed, and _normalBitmap.Handle is inexplicably IntPtr.Zero!

To solve the problem, add an environment.txt to the project with a build action of AndroidEnvironment:

MONO_GC_PARAMS=bridge-implementation=old

Using the old GC implementation, the crash doesn't occur!

Expected Behavior

Although a lot of objects and memory are in use, a crash should not occur here.

Actual Behavior

The app crashes with ObjectDisposedException. I think this issue has plagued various Xamarin.Forms apps (maybe others).

Version Information

I used VS Windows 15.7.6, but this likely will occur with any recent version of Xamarin.Android.

Log File

I enabled GREF logging:

> adb shell setprop debug.mono.log gref,gc

Once the app was running and in a crashed state, I noted _normalBitmap.key_handle is 0xefb562c.

Then I got the log from the device:

> adb shell run-as AndroidControlGallery.AndroidControlGalleryc cat files/.__override__/gref.txt > gref.txt

After several seconds, I could open gref.txt and find 0xefb562c:

handle 0x2f3a; key_handle 0xefb562c: Java Type: `android/graphics/Bitmap`; MCW type: `Android.Graphics.Bitmap`
+g+ grefc 549 gwrefc 0 obj-handle 0x75/I -> new-handle 0x2f2a/G from thread '(null)'(1)
  at Android.Runtime.AndroidObjectReferenceManager.CreateGlobalReference (Java.Interop.JniObjectReference value) [0x00000] in <09e6cd2313ba450ea2148becdafef5de>:0 
  at Java.Interop.JniObjectReference.NewGlobalRef () [0x00000] in <8bda597ae6554278bc49a83870d44290>:0 
  at Android.Runtime.JNIEnv.NewGlobalRef (System.IntPtr jobject) [0x00000] in <09e6cd2313ba450ea2148becdafef5de>:0 
  at Java.Lang.Object.RegisterInstance (Android.Runtime.IJavaObject instance, System.IntPtr value, Android.Runtime.JniHandleOwnership transfer, System.IntPtr& handle) [0x00000] in <09e6cd2313ba450ea2148becdafef5de>:0 
  at Java.Lang.Object.SetHandle (System.IntPtr value, Android.Runtime.JniHandleOwnership transfer) [0x00000] in <09e6cd2313ba450ea2148becdafef5de>:0 
  at Android.Graphics.Canvas..ctor (Android.Graphics.Bitmap bitmap) [0x00000] in <09e6cd2313ba450ea2148becdafef5de>:0 
  at Xamarin.Forms.Platform.Android.ButtonDrawable.CreateBitmap (System.Boolean pressed, System.Int32 width, System.Int32 height) [0x0000e] in C:\Users\jopepper\Desktop\Git\Xamarin.Forms\Xamarin.Forms.Platform.Android\Renderers\ButtonDrawable.cs:164 
  at Xamarin.Forms.Platform.Android.ButtonDrawable.Draw (Android.Graphics.Canvas canvas) [0x0006b] in C:\Users\jopepper\Desktop\Git\Xamarin.Forms\Xamarin.Forms.Platform.Android\Renderers\ButtonDrawable.cs:75 
  at Android.Graphics.Drawables.Drawable.n_Draw_Landroid_graphics_Canvas_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_canvas) [0x00000] in <09e6cd2313ba450ea2148becdafef5de>:0 
  at System.Object.1036a56f-c50a-4b20-ad6d-7ad288fe3c1a (System.IntPtr , System.IntPtr , System.IntPtr ) [0x00000] in <439430c613cf423a807ddc03bf0abc24>:0 

Tracking the handles as GC's occur:

*take_weak obj=0xcfc8be20; handle=0x2f3a
+w+ grefc 591 gwrefc 120 obj-handle 0x2f3a/G -> new-handle 0x92b/W from thread 'finalizer'(15202)

I eventually get to a deadend here:

*try_take_global obj=0xcfc8be20 -> wref=0x92b handle=0x0
-w- grefc 283 gwrefc 390 handle 0x92b/W from thread 'finalizer'(15202)

Which would indicate why _normalBitmap.Handle is IntPtr.Zero.

Full gref log: grefs.zip

Here is the contents of VS debug output window:
debug-output.txt

Mono Runtime

All 14 comments

/cc @lambdageek

@lambdageek let me know if you have trouble building Xamarin.Forms. @luhenry tried sometime recently and was having trouble because it didn't build on latest Mono.

We might need to rebase this branch against XF latest master: https://github.com/xamarin/Xamarin.Forms/compare/handle-testing

@jonathanpeppers Hm. I'm getting stuck on the "build Xamarin.Forms.sln" step.

Getting some MissingMethodException:

/Users/alekseyk/work/bugs/tarjan/Xamarin.Forms/Xamarin.Forms.Core/Xamarin.Forms.Core.csproj(3,3): Error MSB4018: The "FixXFCoreAssembly" task failed unexpectedly.
System.MissingMethodException: Method not found: void System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare)
  at Mono.Cecil.ModuleDefinition.ReadModule (System.String fileName, Mono.Cecil.ReaderParameters parameters) [0x00008] in <e90892cdd8c7498085fa6bd61a0ec43a>:0 
  at Mono.Cecil.AssemblyDefinition.ReadAssembly (System.String fileName, Mono.Cecil.ReaderParameters parameters) [0x00000] in <e90892cdd8c7498085fa6bd61a0ec43a>:0 
  at XFCorePostProcessor.Tasks.FixXFCoreAssembly.Execute () [0x000a8] in <109fc16bdb104d05842432f6682a28fa>:0 
  at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute () [0x00023] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-06/external/bockbuild/builds/msbuild-15/src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs:573 
  at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask (Microsoft.Build.BackEnd.ITaskExecutionHost taskExecutionHost, Microsoft.Build.BackEnd.Logging.TaskLoggingContext taskLoggingContext, Microsoft.Build.BackEnd.TaskHost taskHost, Microsoft.Build.BackEnd.ItemBucket bucket, Microsoft.Build.BackEnd.TaskExecutionMode howToExecuteTask) [0x001f6] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-06/external/bockbuild/builds/msbuild-15/src/Build/BackEnd/Components/RequestBuilder/TaskBuilder.cs:784  (MSB4018) (Xamarin.Forms.Core)
(My Version Info)
=== Visual Studio Enterprise 2017 for Mac ===

Version 7.7.1 (build 15)
Installation UUID: c1b3b394-68d8-4231-92e8-47d068034506
    GTK+ 2.24.23 (Raleigh theme)
    Xamarin.Mac 4.4.1.178 (master / eeaeb7e6)

    Package version: 516000221

=== Mono Framework MDK ===

Runtime:
    Mono 5.16.0.221 (2018-06/b63e5378e38) (64-bit)
    Package version: 516000221

=== NuGet ===

Version: 4.7.0.5148

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
    2.1.6
    2.1.2
    2.1.1
    2.1.0
    2.0.7
    2.0.6
    2.0.5
SDK: /usr/local/share/dotnet/sdk/2.1.500/Sdks
SDK Versions:
    2.1.500
    2.1.302
    2.1.301
    2.1.300
    2.1.200
    2.1.103
    2.1.4
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.16.0/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.6.4
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: 9.1.4.2 (Visual Studio Enterprise)
Android SDK: /Users/alekseyk/Library/Developer/Xamarin/android-sdk-macosx
    Supported Android versions:
        4.0.3 (API level 15)
        4.4   (API level 19)
        5.0   (API level 21)
        6.0   (API level 23)
        7.0   (API level 24)
        7.1   (API level 25)
        8.0   (API level 26)
        8.1   (API level 27)

SDK Tools Version: 26.0.2
SDK Platform Tools Version: 26.0.0
SDK Build Tools Version: 23.0.3

Java SDK: /Users/alekseyk/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.9
openjdk version "1.8.0-9"
OpenJDK Runtime Environment (build 1.8.0-9-microsoft-b00)
OpenJDK 64-Bit Server VM (build 25.71-b00, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android Device Manager ===

Version: 7.7.1.0
Hash: 06ceaea1

=== Apple Developer Tools ===

Xcode 10.1 (14460.46)
Build 10B61

=== Xamarin.Mac ===

Version: 5.2.1.12 (Visual Studio Enterprise)
Hash: 65ec520f
Branch: 
Build date: 2018-12-05 12:06:09-0500

=== Xamarin.iOS ===

Version: 12.2.1.12 (Visual Studio Enterprise)
Hash: 65ec520f
Branch: d15-9
Build date: 2018-12-05 12:06:09-0500

=== Xamarin Inspector ===

Version: 1.4.3
Hash: db27525
Branch: 1.4-release
Build date: Mon, 09 Jul 2018 21:20:18 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 707010015
Git revision: a7ca4daaf7add8fe05ed137fdd51bd9eb77758a7
Build date: 2018-12-05 20:15:35+00
Build branch: release-7.7
Xamarin extensions: 40f5fc1b44727b10b1e7bb1ada3aae33516569ce

=== Operating System ===

Mac OS X 10.14.2
Darwin 18.2.0 Darwin Kernel Version 18.2.0
    Mon Nov 12 20:24:46 PST 2018
    root:xnu-4903.231.4~2/RELEASE_X86_64 x86_64

=== Enabled user installed extensions ===

AddinMaker 1.4.3

Yeah @lambdageek I think they fixed it by using a different overload of a method in Mono.Cecil.

Does it rebase cleanly on Xamarin.Forms master?

@jonathanpeppers it didn't rebase cleanly. but I'm getting stuck earlier than that, even. Not having much success building XF master (or even 3.4.0) from source... Xamarin.Forms.Build.Tasks refuses to build because System.CodeDom isn't compatible with net46.

I don't suppose there's any way to build the controls gallery sample against XF nugets and still reliably get the exception?

@lambdageek I am having trouble myself, I'm talking to the XF team and we'll get you a way to repro this.

@lambdageek try this branch

If you get any errors about Resource.ID not found just rebuild Xamarin.Forms.ControlGallery.Android and that should fix. I added a rebuild command into the shell so it should crash first time you run.

I tested this on an API 23 and API 25 emulator and they both crashed but if you're having issues then try using a device that doesn't have much memory.

I also tested this on a Mac that has all stable and a Mac that has all internal. It crashed the same on both

@BrzVlad can we backport https://github.com/mono/mono/pull/18239 to mono 2019-12?

I'm not that eager to backport the fix, since I would rate it as having a moderate risk. Especially given this issue is very old and has an easy workaround (the old/new bridge). Regarding whether we should backport, I would let @jonpryor and @marek-safar chip in with an opinion.

I'm facing a similar crash in my app. Has this fix been released?

To add a detail about release inclusion, the latest Preview version of Xamarin.Android 10.3.0.33 does now include this fix. The Xamarin.Android release notes tooling didn't find it originally because by chance the fix in mono/mono@376c46bcf3bc101fba85a402914d60c6bda70e75 didn't reference any issues. But I can verify that the commit is present in Xamarin.Android 10.3.0.33, which uses the Mono runtime and class libraries from mono/mono@d90665a422e9f8d015585b3ca381d74faa033cc4.

I will accordingly add this item to the release tracking for that release.


Release status update

A new Preview version has now been published that includes the fix for this item. The fix is not yet included in a Release version. I will update this item again when a Release version is available that includes the fix.

Fix included in Xamarin.Android 10.3.0.33

Fix included on Windows in Visual Studio 2019 version 16.6 Preview 2. To try the Preview version that includes the fix, check for the latest updates in Visual Studio Preview.

Fix included on macOS in Visual Studio 2019 for Mac version 8.6 Preview 1. To try the Preview version that includes the fix, check for the latest updates on the Preview updater channel.

Release status update

A new Release version of Xamarin.Android has now been published that includes the fix for this item.

Fix included in Xamarin.Android 10.3.1.0.

Fix included on Windows in Visual Studio 2019 version 16.6. To get the new version that includes the fix, check for the latest updates or install the latest version from https://visualstudio.microsoft.com/downloads/.

Fix included on macOS in Visual Studio 2019 for Mac version 8.6. To get the new version that includes the fix, check for the latest updates on the Stable updater channel.

Was this page helpful?
0 / 5 - 0 ratings