Bug 5366 - Ver 4.2.2 - System.ObjectDisposedException using FileStream.Length and MemoryStream.Length
Summary: Ver 4.2.2 - System.ObjectDisposedException using FileStream.Length and Memory...
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.2.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2012-05-28 06:01 UTC by Derek Mitchell
Modified: 2012-05-31 13:41 UTC (History)
2 users (show)

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:

Description Derek Mitchell 2012-05-28 06:01:17 UTC
Please note this is an issue introduced for Ver 4.2.2!!!

In ver 4.2.1 - the code works fine.

I'm getting the following error in ver 4.2.2:

System.ObjectDisposedException: The object was used after being disposed.

when calling fs.Length in the code below.

using (Android.Graphics.Bitmap bitmap = Android.Graphics.BitmapFactory.DecodeByteArray(jpegData, 0, jpegData.Length))
    using (System.IO.FileStream fs = new System.IO.FileStream(Program.AppImageFileSpec, System.IO.FileMode.Create))
        bitmap.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 75, fs);
        var sizeInfo = fs.Length;// System.ObjectDisposedException: The object was used after being disposed.

The same also occurs in this code dealing with the MemoryStream:
using (MemoryStream ms = new MemoryStream())
    bitmap.Compress(Bitmap.CompressFormat.Jpeg, 75, ms);
    TaskAssignment.TaskImage.ImageAsImage = ms.ToArray();
    sizeInfo = DBT.helpers.ceHelper.GetSpaceDescription((ulong)ms.Length);

Looking in the 4.2.2 notes I saw this: 

Reduce lifetime of wrapped Streams in InputStreamAdapter, InputStreamInvoker, OutputStreamAdapter, OutputStreamInvoker. Disposing of the wrapper instance will now dispose the wrapped instance. 

Is there any chance this is related?

Is there an interim workaround where I can get the Length?
Comment 1 Jonathan Pryor 2012-05-31 13:41:35 UTC
This should be fixed by commit d5363f65, and will be fixed in the 4.2.3 release.

I'm sorry for the inconvenience.