Bug 56013 - Native Object Is Disposed When It Shouldn't Have
Summary: Native Object Is Disposed When It Shouldn't Have
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: unspecified
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
Depends on:
Reported: 2017-05-04 23:29 UTC by Hakan
Modified: 2017-08-23 21:04 UTC (History)
3 users (show)

Tags: bb
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 Hakan 2017-05-04 23:29:34 UTC
The code below is run in a seperate thread and causes exception at "rotatedImage.Compress(Bitmap.CompressFormat.Jpeg, quality, stream);" stating that it is trying to access a disposed Bitmap. Apparently, when scaledImage is assigned to rotatedImage, the corresponding native bitmap is disposed since scaledImage is not used after this point but the truth is rotatedImage points to the scaled image bitmap and thus the bitmap should not be disposed.

Bitmap scaledImage = BitmapFactory.DecodeFile(filePath, options);
Bitmap rotatedImage = null;

 if (rotation != 0)
                    var matrix = new Matrix();
                    rotatedImage = Bitmap.CreateBitmap(scaledImage, 0, 0, scaledImage.Width,
                        scaledImage.Height, matrix, true);
                    rotatedImage = scaledImage;

int finalWidth = rotatedImage.Width;
int finalHeight = rotatedImage.Height;

using (var stream = new MemoryStream())
                        rotatedImage.Compress(Bitmap.CompressFormat.Jpeg, quality, stream);
                        finalImageByteArray = stream.ToArray();
Comment 1 Mark McLemore 2017-06-26 22:16:29 UTC
Thank you for taking the time to submit the bug. We are unable to reproduce this issue from the description. 

Can you let us know which version of Xamarin.Android and Visual Studio (or Xamarin Studio) you are using? Also, please attach a reproduction to the bug by starting with a clean Xamarin.Android project and adding just the code necessary to demonstrate the issue.

The more information we have, the faster we can resolve the problem -- some guidelines can be found here:  https://developer.xamarin.com/guides/cross-platform/troubleshooting/questions/howto-file-bug/
Comment 2 Cody Beyer (MSFT) 2017-08-23 21:04:24 UTC
Because we have not received a reply to our request for more information we are closing this issue. If you are still encountering this issue, please reopen the ticket with the requested information. Thanks!