   SlimDX GDI Objects leak when passing Rectangle to Device Present
  I've spent several hours this evening chasing this one.I'm using SlimDX within a user control so that I can host a DirectX environment within a WinForms app.I'm using the overload of Present which allows me to specify sourceRectangle and destinationRectangle as well as region.  I set all of these to be the dimensions of my user control at the time of render like this:Dim rRenderArea As New Rectangle(0, 0, Math.Max(16, Width), Math.Max(16, Height))Device.Present(rRenderArea, rRenderArea, Handle, New Region(rRenderArea))Watching Task Manager I see my application process' GDI Objects count increment by 2 (approx) EVERY FRAME!If I change to just use:Device.Present()Then this doesn't happen (GDI Objects stays at 43 rather than going in to the hundreds and then thousands within seconds).I even tried this instead:Dim r1 As New Rectangle(0, 0, Math.Max(16, Width), Math.Max(16, Height))Dim r2 As New Rectangle(0, 0, Math.Max(16, Width), Math.Max(16, Height))Dim r3 As New Rectangle(0, 0, Math.Max(16, Width), Math.Max(16, Height))Device.Present(r1, r2, Handle, New Region(r3))With exactly the same result!I'm keen to use this Present overload so that I don't need to lose/reset my device every time the user scales the control (i.e. my present parameters are set up to have a back buffer width and height which is large enough to cover the entire adapter).Tearing my hair out here and can't see that this is anything I've done wrong.  I certainly can't Dispose of a System.Drawing.Rectangle object so this must be a COM/GDI problem in either SlimDX or DirectX.Any ideas anybody?--edited: additional information--Sorry, forgot to mention that I also downloaded the trial version of GlowCode which was what pointed me towards looking at swapping the Present call out.  What GlowCode showed me when I asked it to track GDI calls was that the offending call is CreateRectRgn.Thanks.   
  At the very least, it doesn't appear to be anything in SlimDX; we don't really do anything other than translate the Rectangles to RECTs and make the call.   ;  This line of code is likely the problem:Device.Present(rRenderArea, rRenderArea, Handle, New Region(rRenderArea)System.Drawing.Region implements IDisposable, but you're never calling Dispose() on it. If you don't call Dispose(), it doesn't get Dispose()'d until it gets finalized (assuming the class implements IDisposable in the canonical fashion), which means that the GDI handle for the region is not passed to DeleteObject() in a timely fashion, and thus leaks.   ;  Thanks for the quick responses guys.I just tried Josh's fix by changing my code to this:Dim rRenderArea As New Rectangle(0, 0, Math.Max(16, Width), Math.Max(16, Height))Dim rgnRenderArea As New Region(rRenderArea)Device.Present(rRenderArea, rRenderArea, Handle, rgnRenderArea)rgnRenderArea.Dispose()rgnRenderArea = NothingBut, alas, GDI Objects count is still hurtling up at what (as far as the eye can tell watching Task Manager!) is the same rate.Other (possibly pertinent) information:OS: Windows XP Professional x64 Edition (Quad Q8200 2.33GHz CPU with 8GB memory).GPU: ATI Radeon HD 4800 Series with 2GB memory.IDE: Visual Studio 2008.Using a SlimDX.Direct3D9.Device (kinda obvious as I'm on XP!).I'ld really like to try and fix this as the only other option is to trash all my unmanaged textures and call lost device on everything every time the user wants to scale the control which seems unnecessary and surely can't scale that well (in terms of responsiveness as the app's DX resource requirements get more complex).   ;  Look for other places you're using GDI (System.Drawing, mostly) objects. The Present() overload you're calling only makes one allocating call into the GDI API (it creates a Graphics object) which it deletes. The cleanup is not exception safe, but that doesn't appear to be an issue. The only other potential source of the leak in the Present() overload is a call to GetHrgn(), however this returns an IntPtr which would not be releasable, the documentation makes no mention of this function transferring ownership or release responsibility, and the actual implementation of GetHrgn simply returns the underlying HRGN without copying (so there really is nothing for us, the GetHrgn() caller, to release).It's either elsewhere in your code, directly, or elsewhere in SlimDX via a call you're making in your code that you haven't shown us. I don't see any evidence that the corrected code you've just posted is still responsible for a leak.Is the leak still coming from a call to CreateRectRgn?   ;  Thanks for the quick response again Josh - much appreciated...I've been working on something else today so have not looked at this further.  What I think I need to do now is create a new project with the minimum number of calls to see if I can reproduce the issue on a smaller scale (I'm experimenting with pixel shaders at the same time and could well be mucking something up in my usage there).All I can promise you is that the only change I make to make the issue disappear is to change the call to Present.I shall return when I've dug deeper at my end.   ;  There have have been cases where GDI mamagement appears to have been broken, so that's another possibility. I suppose we could try calling DeleteObject on the result of GetHrgn(), but I'm very skeptical of making such a change permanently.   ;  Un-retiring thread by OP's request.   