1

I am getting OutofMemoryException in this particular code.

public BitmapImage GetImage(int pageNo)
        {
            if (!this._isLoaded)
            {
                this.Load();
            }
            using (IsolatedStorageFileStream stream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(this.FileNames[pageNo], FileMode.Open, FileAccess.Read))
            {
                BitmapImage image = new BitmapImage();
                image.SetSource(stream);            

                return image;

            }
        }

The out of memory exception is occuring at image.SetSource(stream) . I cant set the uri to null because I have to return the image.

What is the workaround for this? Help me here.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Shilpa
  • 412
  • 3
  • 14
  • Please try the solution from http://stackoverflow.com/questions/10319447/release-handle-on-file-imagesource-from-bitmapimage because it is possible, that you close your stream before it is initialized. – Dmitrii Dovgopolyi Mar 29 '13 at 09:15
  • I cannot find BeginInit, CacheOption , EndInit properties for my Bitmap Image. I am doing this for wp 7. – Shilpa Mar 29 '13 at 09:25
  • How big is the image? – Anton Sizikov Mar 29 '13 at 16:10
  • @AntonSizikov 1060x1500 is the resolution of the image. – Shilpa Apr 02 '13 at 12:47
  • Does in happen on first call? I mean, what is the workflow? Do you create this image once? – Anton Sizikov Apr 02 '13 at 13:07
  • @AntonSizikov I am maintaining a cache of 6 such images in a List.So i can say not one , At the max six images. – Shilpa Apr 03 '13 at 06:28
  • Have a look at this page, http://blogs.developpeur.org/kookiz/archive/2013/02/17/wpdev-memory-leak-with-bitmapimage.aspx hope it will help you – Anton Sizikov Apr 03 '13 at 07:26
  • @AntonSizikov I read the link.Thanks, But I am not very clear regarding the usage of the Dispose(image) method given in the link. How and where should i add it in my code. Since i have to return the image i cannot make it null. – Shilpa Apr 04 '13 at 09:02
  • I think that you show this images, don't you? So, you have an tag in xaml, you load one image, then you load second. Before loading the second image you need to dispose first image. – Anton Sizikov Apr 04 '13 at 13:45
  • @AntonSizikov I resolved the issue finally . As your suggestion i have made the cleared the uri. Thanks for the help – Shilpa Apr 05 '13 at 12:53

1 Answers1

3

I had this list of bitmap images.

 private List<BitmapImage> _images = new List<BitmapImage>();

I cleared the uri while leaving the page.

 protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);
            this.DataContext = null;
            foreach (var obj in this._images)
            {
                if (obj != null)
                {
                    obj.ClearValue(BitmapImage.UriSourceProperty);
                }

            }
Shilpa
  • 412
  • 3
  • 14