Silverlight: BitmapImage from thread exception exception (Crash (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)))

I need to dynamically load many (sometimes hundreds) thumbnails. For performance reasons, I need to do this in a limited number of requests, I use a single request / response for testing. I send binary data for images in response and upload it to BitmapImage using a MemoryStream. This works correctly until I load more than 80 thumbnails, and then get the Catastrophic Error exception. To make sure my data is not corrupted, I tried several times to load BitmapImage with the same byte array, and it will work after loading about 80.

The following is an example of how an image is loaded from an array of bytes. The byte array is known to have the correct image data (png):

private BitmapImage LoadImage(byte[] imageData) { BitmapImage img = new BitmapImage(); MemoryStream stream = new MemoryStream(imageData); img.SetSource(stream); // Exception thrown here after too many images loaded. return img; } 

Then I use BitmapImage as the source for the image element on the page, but the error occurs in the line img.SetSource(...) above.

Adding GC.Collect() to the loop where I upload the thumbnails allows a few more images to be loaded, so I think this has something to do with memory management, but I don't know what I can do to fix the problem.

+7
source share
2 answers

I think the quote from the answer provided by Microsoft in the error message above is worth it, as it is a very brief and descriptive problem, as well as providing a recommended solution:

When Silverlight downloads an image, the environment stores the link and caches the decoded image until flow control is returned to the UI thread manager. When you upload images in such a closed loop, even if your application does not save the link, GC cannot release the image until we release our link when the flow control returns.

After processing 20 or so images, you can stop and queue the next set using Dispatcher.BeginInvoke to break up the work that is being processed in one batch. This will allow us to free images that are not saved by the application.

I understand that with the current decoding behavior, it is not obvious that Silverlight retains these links, but changing the design of the decoder can affect other areas, so at the moment I recommend processing images like this in batches.

Now, if you are actually trying to load 500 images and save them, you may still have a lack of memory depending on the size of the image. If you are dealing with a multi-page document, you can instead load pages on demand in the background and release them when they are not visible with multiple pages of the buffer, so in no case do you exceed the permissible limits of texture memory.

+6
source

I sent an error report with Microsoft on this issue: Catastrophic error exception that occurred after loading too many BitmapImage objects from the stream .

Currently, I will try to get around this using smaller thumbnail image files and / or not loading as many BitmapImages (unloading images when they are not in the visible area, and reloading them when they come).

0
source

All Articles