I am creating an app for Window 8 using the ‘Windows Runtime’.
I have a custom control which does it’s own painting. To accomplish this, I am using a Rectangle control and setting it’s ‘Fill’ property to an ImageBrush derived from a SurfaceImageSource and then using an ID2D1RenderTarget to do the painting. For painting bitmaps I am using ID2D1RenderTarget::CreateBitmapFromWicBitmap and am saving the ID2D1Bitmap that is returned. The control is animated and I create and all of the bitmaps that I need and then continuously paint the scene.
This all works well for the most part. However I am finding that on a random basis, suddenly all of the ID2D1Bitmaps that I have squirreled away stop painting anything. I can trace through the code, and there seems to be nothing wrong, but when I call ID2D1RenderTarget::DrawBitmap, nothing happens. I find, however, that if I force the ID2D1Bitmap to be recreated from the file, it then works fine again. It is almost as if the bitmap has been invalidated or something.
This loss of the bitmaps happens very infrequently and appears to be random, but I can pretty much guarantee that it will fail within an hour or so of playing. I am pretty sure that it is not a memory leak because I can run the app for hours at a time without any increase in memory used. Also, if I force the bitmaps to reload, I can then continue on as if nothing had happened.
Can anyone give me a clue as to what might be going on here?
Thanks.
After much trial and error (not helped by the fact that it was VERY difficult to reproduce the problem), I discovered that the root of the problem was that the call to ID2DRenderTarget::BeginDraw was returning DXGI_ERROR_DEVICE_REMOVED. In the example code upon which I based my code, it showed that in response to this error, I needed to re-create the context, which I was doing. However I was not deleting all of the cached bitmaps associated with the old context, and those bitmaps would not work with the new context. I fixed that and now my app recovers from this error.
Although this solves the problem, I am still not sure I understand why I am getting the ‘Device Removed’ errors at random times while playing. Also, needless to say, that reloading all of the graphics in the middle of playing shows up as a nasty delay glitch. Fortunately it happens rarely, so I guess I will have to live with it.