Bug 51789 - MemoryStream not removed from memory
Summary: MemoryStream not removed from memory
Status: RESOLVED ANSWERED
Alias: None
Product: Forms
Classification: Xamarin
Component: Forms ()
Version: 2.3.3
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-01-26 15:03 UTC by Gert Jan Schoneveld
Modified: 2017-03-28 21:38 UTC (History)
3 users (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:
RESOLVED ANSWERED

Description Gert Jan Schoneveld 2017-01-26 15:03:03 UTC
First I create an Image from a MemoryStream like so:
var enc = "<imageAsBase64>"; // see link to forum or my code below for real example
var arg = System.Convert.FromBase64String(enc);
var ms = new LoggingMemoryStream(arg);
var img = new LoggingImage();
var src = ImageSource.FromStream(() => ms);
img.Source = src;
stack.Children.Add(img);

Running in the simulator on iOS 10.2: When the Image gets out of scope, its destructor is called like expected. But the destructor of the underlying MemoryStream is never being called. To me that seems to be a bug and a possible memory leak.

See also:
https://forums.xamarin.com/discussion/87193/ios-memorystream-not-removed-from-memory

Example:
https://github.com/gjschoneveld/ImageFromStream
Comment 1 Paul DiPietro [MSFT] 2017-03-28 21:38:12 UTC
~LoggingMemoryStream() is implicitly calling the finalizer (https://msdn.microsoft.com/en-us/library/66x5fx1b.aspx), but there are no guarantees on when it occurs. If you override Dispose in the LoggingMemoryStream class and add a breakpoint, you'll notice that it's being called, so presumably it's already destroyed before the finalizer would even need to be called. I don't believe this is an issue. If you want to safely guarantee its disposal you can also wrap it in a using block.