GDI+ Problem with OutOfMemoryException rasied sometime from System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)Hi,<br><br>Me, and users of the .NET application I am working on, sometime get an OutOfmemoryException which callstack ends up with:<br> <p>Exception.Type {System.OutOfMemoryException}<br>Exception.Message {Out of memory.}<br>Exception.StackTrace { <br>at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)<br></p><br>I personally works with Windows Vista Ultimate x64, .NET 3.5 SP1, but it doesn't seems to comes from a particular version of the OS and of .NET since this problem sometime appears on x32 machines with .NET 2.0.<br><br>Example of call stacks are:<br><br>at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)<br>   at System.Drawing.Font.GetHeight()<br>   at System.Drawing.Font.get_Height()<br><br>or<br>at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)<br>   at System.Windows.Forms.PaintEventArgs.get_Graphics()<br>   at System.Windows.Forms.Control.PaintBackColor(PaintEventArgs e, Rectangle rectangle, Color backColor)<br>   at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)<br>   at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)<br>   at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)<br>   at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e)<br>   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)<br>   at System.Windows.Forms.Control.WmEraseBkgnd(Message&amp; m)<br>   at System.Windows.Forms.Control.WndProc(Message&amp; m)<br>   at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)<br>   at System.Windows.Forms.ContainerControl.WndProc(Message&amp; m)<br>   at System.Windows.Forms.SplitContainer.WndProc(Message&amp; msg)<br>   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)<br>   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)<br><br>or<br><br>at System.Drawing.Graphics.<span class=nfakPe>FromHdcInternal</span>(IntPtr hdc)<br>   at System.Drawing.Font.ToLogFont(Object logFont)<br>   at System.Windows.Forms.RichTextBox.FontToLogFont(Font value, LOGFONT logfont)<br>   at System.Windows.Forms.RichTextBox.SetCharFormatFont(Boolean selectionOnly, Font value)<br>   at System.Windows.Forms.RichTextBox.set_SelectionFont(Font value)<br><br>Reflector tells me that it is the win32 methods GdipCreateFromHDC() from gdiplus.dll that returns a status equals to 3.<br><br>The problem happens rarely, but often enough to bothers some our users.<br>Could it be a lack of Dispose() on some handles on GDI+ objects?<br><br>Any help would be highly appreciated.<br>Thanks,<br><br>Patrick Smacchia<br>MVP Visual C#<br>Lead Developer of NDepend     http://www.NDepend.com<br>Author of:    Practical .NET2 and C#2  http://www.practicaldot.net <br> <hr size="1" align="left" width="25%">Patrick (at) Smacchia (dot) com© 2009 Microsoft Corporation. All rights reserved.Fri, 05 Sep 2008 07:09:46 Z7c4d2e73-6e73-4f10-a614-13fd76b2f419http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419#7c4d2e73-6e73-4f10-a614-13fd76b2f419http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419#7c4d2e73-6e73-4f10-a614-13fd76b2f419Patrick Smacchiahttp://social.msdn.microsoft.com/Profile/en-US/?user=Patrick%20SmacchiaGDI+ Problem with OutOfMemoryException rasied sometime from System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)Hi,<br><br>Me, and users of the .NET application I am working on, sometime get an OutOfmemoryException which callstack ends up with:<br> <p>Exception.Type {System.OutOfMemoryException}<br>Exception.Message {Out of memory.}<br>Exception.StackTrace { <br>at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)<br></p><br>I personally works with Windows Vista Ultimate x64, .NET 3.5 SP1, but it doesn't seems to comes from a particular version of the OS and of .NET since this problem sometime appears on x32 machines with .NET 2.0.<br><br>Example of call stacks are:<br><br>at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)<br>   at System.Drawing.Font.GetHeight()<br>   at System.Drawing.Font.get_Height()<br><br>or<br>at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)<br>   at System.Windows.Forms.PaintEventArgs.get_Graphics()<br>   at System.Windows.Forms.Control.PaintBackColor(PaintEventArgs e, Rectangle rectangle, Color backColor)<br>   at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)<br>   at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)<br>   at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)<br>   at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e)<br>   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)<br>   at System.Windows.Forms.Control.WmEraseBkgnd(Message&amp; m)<br>   at System.Windows.Forms.Control.WndProc(Message&amp; m)<br>   at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m)<br>   at System.Windows.Forms.ContainerControl.WndProc(Message&amp; m)<br>   at System.Windows.Forms.SplitContainer.WndProc(Message&amp; msg)<br>   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)<br>   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)<br><br>or<br><br>at System.Drawing.Graphics.<span class=nfakPe>FromHdcInternal</span>(IntPtr hdc)<br>   at System.Drawing.Font.ToLogFont(Object logFont)<br>   at System.Windows.Forms.RichTextBox.FontToLogFont(Font value, LOGFONT logfont)<br>   at System.Windows.Forms.RichTextBox.SetCharFormatFont(Boolean selectionOnly, Font value)<br>   at System.Windows.Forms.RichTextBox.set_SelectionFont(Font value)<br><br>Reflector tells me that it is the win32 methods GdipCreateFromHDC() from gdiplus.dll that returns a status equals to 3.<br><br>The problem happens rarely, but often enough to bothers some our users.<br>Could it be a lack of Dispose() on some handles on GDI+ objects?<br><br>Any help would be highly appreciated.<br>Thanks,<br><br>Patrick Smacchia<br>MVP Visual C#<br>Lead Developer of NDepend     http://www.NDepend.com<br>Author of:    Practical .NET2 and C#2  http://www.practicaldot.net <br> <hr size="1" align="left" width="25%">Patrick (at) Smacchia (dot) comSat, 30 Aug 2008 17:40:30 Z2008-08-30T17:40:30Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419#87f676f7-ac2b-45a1-ac3b-5bc43bc2269fhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419#87f676f7-ac2b-45a1-ac3b-5bc43bc2269fJohnWeinhttp://social.msdn.microsoft.com/Profile/en-US/?user=JohnWeinGDI+ Problem with OutOfMemoryException rasied sometime from System.Drawing.Graphics.FromHdcInternal(IntPtr hdc) I don't think .NET GDI+ is quite ready for prime time.  If you're doing something other than normal control painting, consider calling the GDI API methods directly.Sat, 30 Aug 2008 18:30:59 Z2008-08-30T18:31:56Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419#47af0a34-0615-4f24-9834-ad18488d2bd5http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419#47af0a34-0615-4f24-9834-ad18488d2bd5Patrick Smacchiahttp://social.msdn.microsoft.com/Profile/en-US/?user=Patrick%20SmacchiaGDI+ Problem with OutOfMemoryException rasied sometime from System.Drawing.Graphics.FromHdcInternal(IntPtr hdc) John, so far our application is massively using GDI+ with just one case where this OutOfMemoryException was raised. It seems that only recently the number of occurences of the problem increase.<hr size="1" align="left" width="25%">Patrick (at) Smacchia (dot) comMon, 01 Sep 2008 07:30:02 Z2008-09-01T07:30:02Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419#ca571c5a-4aab-4dd0-a360-b3ed34a26b91http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419#ca571c5a-4aab-4dd0-a360-b3ed34a26b91nobugzhttp://social.msdn.microsoft.com/Profile/en-US/?user=nobugzGDI+ Problem with OutOfMemoryException rasied sometime from System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)GDI+ generates pretty lousy exceptions, you can get OOM in error conditions that have nothing whatsoever to do with running out of memory.  With this call stack, I'd say you are probably running out of GDI handles.  There's a limit of 10,000 handles for a process.  Not calling Dispose() on graphics objects is indeed a good way to trip the problem.  You would have to get into a condition where you do lots of painting but not allocate sufficient managed objects to trip a garbage collection.  That's very hard to do.  Perhaps the real leak is elsewhere, CopyFromScreen() in .NET 2.0 SP1 had a handle leak for example. <hr size="1" align="left" width="25%">Hans Passant.Mon, 01 Sep 2008 14:09:45 Z2008-09-01T14:09:45Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419#47798245-0db0-429c-9fcd-5b58072bad31http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419#47798245-0db0-429c-9fcd-5b58072bad31Patrick Smacchiahttp://social.msdn.microsoft.com/Profile/en-US/?user=Patrick%20SmacchiaGDI+ Problem with OutOfMemoryException rasied sometime from System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)Hans, I think you are right, my process is certainly running out of GDI handles. Meantime, I found this following thread where several programmers have the same problem and an MSFT ends up to the conclusion that some GDI object are not properly disposed before they are collected.<br><a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=161567&amp;SiteID=1">http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=161567&amp;SiteID=1 </a> <hr size="1" align="left" width="25%">Patrick (at) Smacchia (dot) comMon, 01 Sep 2008 14:15:08 Z2008-09-01T14:15:08Z