Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Windows.Media.Imaging.WriteableBitmap leaking memory?

Consider the following code throws an exception 'MILERR_WIN32ERROR':

while(true)
            {
                System.Windows.Media.Imaging.WriteableBitmap writableBitMap =
                                    new System.Windows.Media.Imaging.WriteableBitmap(100, 200, 96, 96, System.Windows.Media.PixelFormats.Bgra32, null);
            }

WriteableBitmap does not implement IDisposable, I'm not sure why the memory leak is happening at all (the original code I had was much more complicated and I tracked it down to this).

like image 212
meds Avatar asked Oct 28 '25 03:10

meds


2 Answers

The memory leak issue is derived from memory leak of BitmapSource. Googling around that will find you better results :)

This says that you should assign writableBitMap to 'null' once you have used it. I would say try using Freeze() method before assigning it to null and then try. If after that, if you see memory leak persist, disable the caching option (CacheOption) by setting it to BitmapCacheOption.OnLoad.

like image 179
Yogee Avatar answered Oct 29 '25 17:10

Yogee


The memory leak issue is derived from memory leak of BitmapSource. Googling around that will find you better results :)

This says that you should assign writableBitMap to 'null' once you have used it. I would say try using Freeze() method before assigning it to null and then try. If after that, if you see memory leak persist, disable the caching option (CacheOption) by setting it to BitmapCacheOption.OnLoad.

like image 28
Yogee Avatar answered Oct 29 '25 17:10

Yogee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!