1
votes

I'm working on a Windows Phone 8 app and I'm having an issue with releasing resources from a file stream. The problem happens when I access the isolated storage to get an image, and then I set the image to an image source on the view; this all happens when the page loads. (I'm using the Windows Phone Application Analysis tool to view the memory usage). Also, whenever I close and reopen the page in the app, the memory usage continues to increase.

This is my code to get the image and set it:

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("background.jpg", FileMode.Open, FileAccess.Read))
{
    BitmapImage imageFile = new BitmapImage();
    imageFile.SetSource(fileStream);
    BackgroundImage.ImageSource = imageFile;

    // tried using .Close() but it wasn't releasing the resources as well. 
    fileStream.Dispose();
}

Is there something else I should be doing to properly release the resources?

EDIT

I realized my problem... When the app first starts I have it setting the same image that I'm opening in the code above. So when I open a new page with the same image, it seems to not be releasing the existing file stream resources because it's opening the same file over and over again. The fix was to add a BitMapImage property in my View Model that I can access whenever I need without having to constantly grab the file in the isolated storage.

Thanks for all the help from everyone

2
Are you sure that the above code makes the problem? Is there any other codes in your page? - asitis

2 Answers

0
votes

You need to dispose the IsolatedStorageFile, too. This should work (though I didn't compile it)

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) {
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("background.jpg", FileMode.Open, FileAccess.Read))
{
    BitmapImage imageFile = new BitmapImage();
    imageFile.SetSource(fileStream);
    BackgroundImage.ImageSource = imageFile;
}
}
0
votes

You need to use Source propety instead of ImageSource:

using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) {
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("background.jpg", FileMode.Open, FileAccess.Read))
{
    BitmapImage imageFile = new BitmapImage();
    imageFile.SetSource(fileStream);
    BackgroundImage.Source= imageFile;
}
}