Memory exception in System.Drawing.Graphics.FromHdcInternal, but without memory leak

It turns out a very random crash with the following trace:

System.OutOfMemoryException: Out of memory. at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc) at System.Windows.Forms.PaintEventArgs.get_Graphics() at System.Windows.Forms.Control.PaintBackColor(PaintEventArgs e, Rectangle rectangle, Color backColor) at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset) at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle) at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent) at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e) at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs) at System.Windows.Forms.Control.WmEraseBkgnd(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ContainerControl.WndProc(Message& m) at System.Windows.Forms.UserControl.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

As you can see, none of my codes were found in the trace, so it is very difficult for me to find the reason. Google searches seem inconclusive, but usually indicate a GC descriptor leak somewhere, but after the last crash, my memory usage was:

 Handle count:16,283, Private Bytes:995,440K, Virtual Bytes:1,628,208K, Working Set:866,892K, GC Heap Size:158,841K, GDI Objects:402, User Objects:1,607 

This does not look unusual. In addition, I regularly use the .net memory profiler to manage leaks.

Unfortunately, my application is quite large with many windows, so my first question is: how can I determine which window causes all the pain?

and then, of course, my second question: If there is no handle leak, what causes an exception ??

Edit:

Sorry, I cannot post the code: its massive code base and the arent exception definitely give me any information about which part might be the problem.

I read that there is a limit of 10,000 on descriptors, but this application has historically always worked fine with 15,000, so I assumed that the restriction was on something else: could there be GDI handlers or user objects?

Just to make sure, I checked and the pens do not leak out, since all of them are assigned at startup and do not increase using.

Let me change my question: Given this information, what should be the next course of action? I have Process Explorer installed and you managed to get a full memory dump from one of the crashes, but in fact I have no experience using either to diagnose this problem (until now, the .net memory profiler was enough)

+4
source share
2 answers

You probably assign brushes or pens and do not dispose of them. This consumes the GDI handles, and at some point they are all used, and you get an OutOfMemoryException.

+3
source

Since we cannot see the code, here is the opportunity for this error. It happened to me.

Make sure you call the correct method. I tried to get the Graphics object from the HDC, and I had to choose the overload method, which is instead of HWND. It is easy to deal with these errors here, since there is no type checking between these handles.

0
source

Source: https://habr.com/ru/post/1341956/


All Articles