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

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:

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.