3

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).

meds
  • 17,850
  • 30
  • 131
  • 265
  • What version of vs and what version of the .net framework are you targeting? – willaien Aug 30 '15 at 14:46
  • This code should be fine (well... by bearing in mind that it is inside an infinite loop, which I understand that does not exist in the original code). After performing a quick search about your error, it seems that it is provoked by a very high number of image objects. So, I guess that you should look at what is happening in the rest of your code. As far as I understand that you are aware that this while(true) is an infinite loop (your actual code should have something inside that loop to break it at some point, otherwise there you have the reason for your error). – varocarbas Aug 30 '15 at 15:10
  • I'm trying to write a program that reads a video file in frame by frame and equally blends the frames together, the code basically goes through a video file and reads each frame, writes it out to a writeablebitmap, blits it into another one then goes on to the next frame, terminating when it has run out of video frames to read. I guess I can look into trying to batch sets of frames together to speed it up. – meds Aug 30 '15 at 15:15
  • Your intention is fine, but the posted code is not doing this. The code you are posting is instantiating WriteableBitmaps over and over without never stopping. When reading a file, the loops are automatically quitted because a given conditions tells them so. With a while(true) you have always to set an exit condition, otherwise the loop would continue forever (better: until the program will crash). – varocarbas Aug 30 '15 at 15:25
  • @varocarbas that doesn't make sense, even if I infinitely loop and create writeable bitmaps there shouldn't be an out of memory crash, the garbage collector should step in and make sure memory is released when writeable bitmap goes out of scope. The code snippet is sufficient to demonstrate the memory issue. – meds Sep 03 '15 at 10:33
  • @soshiki (sorry... it has been a while and I thought this was a different post :)) Let's go step by step: if this is your code, you have an infinite loop, end of the story. If we ignore the loop, this code works fine. Or what is the exact motivation for this comment? Trying to prove that you can create an infinite loop in an application and worry about other things?! No, cannot. Please, soshiki, let's reduce the unnecessary time waste and focus on the real world: you cannot have an infinite loop; this code has an infinite loop; if this is your code, correct it; otherwise post your actual code. – varocarbas Sep 03 '15 at 10:37

1 Answers1

2

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.

Community
  • 1
  • 1
Yogee
  • 1,291
  • 13
  • 20
  • Didn't work and that other stackoverflow link you have is for a different class, writeablebitmap does not have a cache option :( – meds Sep 03 '15 at 10:32
  • Googling this has led me to believe this is a known issue, writeablebitmaps just leak memory and there doesn't seem to be an obvious solution – meds Sep 03 '15 at 10:33