WriteableBitmap memory leak in Windows Phone 8

I have a memory leak whenever I create any instance of WriteableBitmap . I tried several suggestions on stackoverflow and other forums, but nothing works. The main thread of my test application is this:

  • Select an image using PhotoChooserTask
  • Use the Stream from the PhotoResult object to create a WriteableBitmap .

What is it. Dropping variables and calling GC.Collect() solve only part of the problem. It does not allow the application to allocate memory until the application crashes, but even if the objects go out of scope, memory is always allocated for them until I select a new image. I can play it using the default Windows Phone Direct3D with the XAML app. The only changes to the default project are as follows:

MainPage.xaml.cs

 public MainPage() { InitializeComponent(); _photoChooserTask = new PhotoChooserTask(); _photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTaskComplete); } private void ApplicationBarIconButton_Click(object sender, EventArgs e) { _photoChooserTask.Show(); } private void photoChooserTaskComplete(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { BitmapImage image = new BitmapImage(); image.SetSource(e.ChosenPhoto); WriteableBitmap wbm = new WriteableBitmap(image); image.UriSource = null; image = null; wbm = null; GC.Collect(); } } 

MainPage.xaml

 <phone:PhoneApplicationPage.ApplicationBar> <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" Mode="Default" Opacity="0.5" > <shell:ApplicationBar.Buttons> <shell:ApplicationBarIconButton IconUri="/junkUrl.png" Text="albums" Click="ApplicationBarIconButton_Click" /> </shell:ApplicationBar.Buttons> </shell:ApplicationBar> </phone:PhoneApplicationPage.ApplicationBar> 
+6
source share
1 answer

To do this, you need to save this file stream inside IsolStorege. So, create a stream using IsolStorageFileStream and then save it like this ...

 private void photoChooserTaskComplete(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { SaveToIsolatedStorage(e.ChosenPhoto,"Your File Name"); } 

}

  public void SaveToIsolatedStorage(Stream imageStream, string fileName) { try { string imagename = fileName + ".jpg"; using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (myIsolatedStorage.FileExists(imagename)) { myIsolatedStorage.DeleteFile(imagename); } IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(imagename); WriteableBitmap wb = new WriteableBitmap(100, 100); wb.SetSource(imageStream); wb.SaveJpeg(fileStream, 100, 100, 0, 70); fileStream.Close(); } } catch (Exception) { RadMessageBox.Show(String.Empty, MessageBoxButtons.OK, "Error occured while saving Images"); } } 

And for reading you can get this file from IsolStorage

 public WriteableBitmap ReadFromIsolatedStorage(string fileName) { WriteableBitmap bitmap = new WriteableBitmap(100, 100); try { using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) { if (myIsolatedStorage.FileExists(fileName)) { using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read)) { // Decode the JPEG stream. bitmap = PictureDecoder.DecodeJpeg(fileStream, 100, 100); } } } } catch (Exception) { RadMessageBox.Show(String.Empty, MessageBoxButtons.OK, "Error Occcured while reading image"); } return bitmap; } 

This will solve the memory leak problem, try this ...

-1
source

Source: https://habr.com/ru/post/952170/


All Articles