Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] **System.ObjectDisposedException:** 'Can not access a closed Stream.' #479

Closed
takacsalbert opened this issue Mar 19, 2020 · 8 comments · Fixed by #493
Closed

[BUG] **System.ObjectDisposedException:** 'Can not access a closed Stream.' #479

takacsalbert opened this issue Mar 19, 2020 · 8 comments · Fixed by #493
Labels

Comments

@takacsalbert
Copy link

takacsalbert commented Mar 19, 2020

Description
I get the following exception on Android in a Xamarin.Android native app:
System.ObjectDisposedException: 'Can not access a closed Stream.'
The app crashes after starting in debug mode. The app doesn't crash when starting from the icon on the device normally, but doesn't load any images.

StackTrace:
03-19 13:51:50.917 I/MonoDroid( 4650): UNHANDLED EXCEPTION:
03-19 13:51:50.921 I/MonoDroid( 4650): System.ObjectDisposedException: Can not access a closed Stream.
03-19 13:51:50.922 I/MonoDroid( 4650): at System.IO.MemoryStream.EnsureNotClosed () [0x00008] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/Common/src/CoreLib/System/IO/MemoryStream.cs:121
03-19 13:51:50.922 I/MonoDroid( 4650): at System.IO.MemoryStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x0004e] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/Common/src/CoreLib/System/IO/MemoryStream.cs:369
03-19 13:51:50.922 I/MonoDroid( 4650): at Android.Runtime.InputStreamAdapter.Read (System.Byte[] bytes, System.Int32 offset, System.Int32 length) [0x00006] in <4a189ea3b82b48a089ac9002b2abc206>:0
03-19 13:51:50.922 I/MonoDroid( 4650): at Java.IO.InputStream.n_Read_arrayBII (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_b, System.Int32 off, System.Int32 len) [0x00020] in <4a189ea3b82b48a089ac9002b2abc206>:0
03-19 13:51:50.922 I/MonoDroid( 4650): at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.49(intptr,intptr,intptr,int,int)
03-19 13:51:50.924 W/Looper ( 4650): Slow Looper main: Long Msg: seq=148 plan=13:51:26.895 late=97ms wall=23926ms running=896ms runnable=161ms h=android.os.Handler c=mono.java.lang.RunnableImplementor
03-19 13:51:50.930 I/Choreographer( 4650): Skipped 1438 frames! The application may be doing too much work on its main thread.
03-19 13:51:50.938 W/acsalbert.elll( 4650): JNI RegisterNativeMethods: attempt to register 0 native methods for android.runtime.JavaProxyThrowable
03-19 13:51:50.942 D/Mono ( 4650): DllImport searching in: '__Internal' ('(null)').
03-19 13:51:50.942 D/Mono ( 4650): Searching for 'java_interop_jnienv_throw'.
03-19 13:51:50.942 D/Mono ( 4650): Probing 'java_interop_jnienv_throw'.
03-19 13:51:50.942 D/Mono ( 4650): Found as 'java_interop_jnienv_throw'.
03-19 13:51:50.977 W/acsalbert.elll( 4650): Attempt to remove index outside index area (0 vs 1-1)
03-19 13:51:50.977 W/acsalbert.elll( 4650): JNI WARNING: DeleteLocalRef(0x5) failed to find entry
03-19 13:51:50.987 F/acsalbert.elll( 4650): java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: JNI CallStaticObjectMethodA called with pending exception android.runtime.JavaProxyThrowable: System.ObjectDisposedException: Can not access a closed Stream.
03-19 13:51:50.987 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at System.IO.MemoryStream.EnsureNotClosed () [0x00008] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/Common/src/CoreLib/System/IO/MemoryStream.cs:121
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at System.IO.MemoryStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x0004e] in /Users/builder/jenkins/workspace/archive-mono/2019-08/android/release/external/corefx/src/Common/src/CoreLib/System/IO/MemoryStream.cs:369
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at Android.Runtime.InputStreamAdapter.Read (System.Byte[] bytes, System.Int32 offset, System.Int32 length) [0x00006] in <4a189ea3b82b48a089ac9002b2abc206>:0
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at Java.IO.InputStream.n_Read_arrayBII (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_b, System.Int32 off, System.Int32 len) [0x00020] in <4a189ea3b82b48a089ac9002b2abc206>:0
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.49(intptr,intptr,intptr,int,int)
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at int mono.android.runtime.InputStreamAdapter.n_read(byte[], int, int) (InputStreamAdapter.java:-2)
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at int mono.android.runtime.InputStreamAdapter.read(byte[], int, int) (InputStreamAdapter.java:56)
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at android.graphics.Bitmap android.graphics.BitmapFactory.nativeDecodeStream(java.io.InputStream, byte[], android.graphics.Rect, android.graphics.BitmapFactory$Options, long, long) (BitmapFactory.java:-2)
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at android.graphics.Bitmap android.graphics.BitmapFactory.decodeStreamInternal(java.io.InputStream, android.graphics.Rect, android.graphics.BitmapFactory$Options) (BitmapFactory.java:790)
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at android.graphics.Bitmap android.graphics.BitmapFactory.decodeStream(java.io.InputStream, android.graphics.Rect, android.graphics.BitmapFactory$Options) (BitmapFactory.java:765)
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] at android.graphics.Bitmap android.graphics.BitmapFactory.decodeStream(java.io.InputStream) (BitmapFactory.java:806)
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570]
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] in call to CallStaticObjectMethodA
03-19 13:51:50.988 F/acsalbert.elll( 4650): java_vm_ext.cc:570] from int mono.android.runtime.InputStreamAdapter.n_read(byte[], int, int)

Steps To Reproduce
Update Splat and Splat.Drawing to version 9.3.11, and start the application.

Expected behavior
The app doesn't crash.

Versions
Splat version 9.3.11
Splat.Drawing version 9.3.11

Environment

  • OS: Windows 10 (Version 1909, 18363.720)
  • Visual Studio 2019 16.4.5
  • Android physical device 10.0 (Xiaomi Mi MIX 3)
@dpvreony dpvreony added the Bug label Mar 20, 2020
@dpvreony
Copy link
Member

any chance of a mini repro please? also does it do it on the emulator?

@takacsalbert
Copy link
Author

takacsalbert commented Mar 20, 2020

Sorry, updated the wrong issue. Update:

Started to dig into this and finally came to a solution that works fine. I hope you find it useful.
I found the answer here: https://forums.xamarin.com/discussion/16500/bitmap-decode-byte-array-skia-decoder-returns-false

And the solution:
var buffer = imageArray.Concat(new byte[] { (byte)0xFF, (byte)0xD9 }).ToArray();
bitmap = await BitmapFactory.DecodeByteArrayAsync(buffer, 0, buffer.Length);

This works for jpg and png images. I am happy to send a pull request with the fix.

Xamarin Community Forums
Hi everybody, I'm having a problem when transforming a byte array image to a bitmap. Some images are not shown and others yes(always the same images).

@takacsalbert
Copy link
Author

any chance of a mini repro please? also does it do it on the emulator?

I am putting together a sample soon.
Tried on an emulator with API 27, Android 8.1, and the exception occurs for me.
I continue experimenting with other emulators as well.

@takacsalbert
Copy link
Author

The same issue exists on API 23, Android 6.0 emulator too.
It seems to be that it depends on the target API of the project.

@dpvreony
Copy link
Member

the previous fixes related to a new version of xamarin forms. so as long as the emulator doing it as well can stick the test harness app on there to check it :)

@dj-dev
Copy link

dj-dev commented Apr 7, 2020

We are getting the same exception thrown in a .net standard library referenced from a Xamarin.Android project.

Exception

System.ObjectDisposedException: Cannot access a closed Stream.

MemoryStream.EnsureNotClosed () /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/Common/src/CoreLib/System/IO/MemoryStream.cs:121
MemoryStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corefx/src/Common/src/CoreLib/System/IO/MemoryStream.cs:369
InputStreamAdapter.Read (System.Byte[] bytes, System.Int32 offset, System.Int32 length)
InputStream.n_Read_arrayBII (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_b, System.Int32 off, System.Int32 len)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.58(intptr,intptr,intptr,int,int)

nuget/project versions
akavache v6.10.6
Akavache.Drawing v6.10.6
Splat v9.4.1
Splat.Drawing v9.4.1

Xamarin.Android project
Target Android framework: v10.0

.net Standard project
Target framework: .net Standard 2.0

Xamarin.Android project code

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    Locator.CurrentMutable.RegisterPlatformBitmapLoader();

This is the called in the first activity to load when the app starts.

.net Standard project code

image = await BlobCache.LocalMachine.LoadImageFromUrl(imageUrl)

The exception is thrown as a result of the call to LoadImageFromUrl. It can't be caught with try/catch. The stack trace above is taken from Microsoft App Center crash logs.

Environment
Windows 10
Visual Studio 16.5.2
Xamarin 16.5.000.528
Xamarin.Android SDK 10.2.0.100
Target Android devices: S8 emulator and S10 device running Android 10

Worth noting the same error is not present in a Xamarin.iOS app referencing the same .net standard library (also updated to the latest Splat and Akavache versions).

As a temporary fix we have dropped back to akavache v6.5.9 where LoadImageFromUrl was available in this package (not Akavache.Drawing).

@dj-dev
Copy link

dj-dev commented Sep 14, 2020

This is still happening with Akavache 6.10.20 and Akavache.Drawing 6.10.20.

Calling BlobCache.LocalMachine.LoadImageFromUrl in a Xamarin.Android project will result in a "System.ObjectDisposedException: Cannot access a closed Stream" Exception:

09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570] JNI DETECTED ERROR IN APPLICATION: JNI CallStaticObjectMethodA called with pending exception android.runtime.JavaProxyThrowable: System.ObjectDisposedException: Cannot access a closed Stream.
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at System.IO.MemoryStream.EnsureNotClosed () [0x00008] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/IO/MemoryStream.cs:121 
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at System.IO.MemoryStream.Read (System.Byte[] buffer, System.Int32 offset, System.Int32 count) [0x0004e] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/IO/MemoryStream.cs:369 
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at Android.Runtime.InputStreamAdapter.Read (System.Byte[] bytes, System.Int32 offset, System.Int32 length) [0x00006] in <9a14097a65a445eaa85f6a3a1ade52a3>:0 
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at Java.IO.InputStream.n_Read_arrayBII (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_b, System.Int32 off, System.Int32 len) [0x00019] in <9a14097a65a445eaa85f6a3a1ade52a3>:0 
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.19(intptr,intptr,intptr,int,int)
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at int mono.android.runtime.InputStreamAdapter.n_read(byte[], int, int) (InputStreamAdapter.java:-2)
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at int mono.android.runtime.InputStreamAdapter.read(byte[], int, int) (InputStreamAdapter.java:56)
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at android.graphics.Bitmap android.graphics.BitmapFactory.nativeDecodeStream(java.io.InputStream, byte[], android.graphics.Rect, android.graphics.BitmapFactory$Options, long, long) (BitmapFactory.java:-2)
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at android.graphics.Bitmap android.graphics.BitmapFactory.decodeStreamInternal(java.io.InputStream, android.graphics.Rect, android.graphics.BitmapFactory$Options) (BitmapFactory.java:875)
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at android.graphics.Bitmap android.graphics.BitmapFactory.decodeStream(java.io.InputStream, android.graphics.Rect, android.graphics.BitmapFactory$Options) (BitmapFactory.java:850)
09-14 17:23:09.320 F/avacheimagetes(29589): java_vm_ext.cc:570]   at android.graphics.Bitmap android.graphics.BitmapFactory.decodeStream(java.io.InputStream) (BitmapFactory.java:891)

Here is an example project to replicate the issue. It's a basic "out of the box" Xamarin.Android project following the basic setup steps for Akavache. The example image url points to the ReactiveUI logo on the ReactiveUI homepage.

AkavacheImageTest.zip

Once the app has loaded, tap the icon at the bottom right of the main screen to call LoadImageFromUrl(). An exception will be thrown on line 62 in MainActivity.cs.

I am debugging directly on a Samsung S9 device running Android 10.

If anyone has a chance to look into this that would be most appreciated. In the mean time, we have dropped back to akavache v6.5.9 (where LoadImageFromUrl was available in this package and not Akavache.Drawing).

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants