How to resolve winforms error "General error occurred in GDI +".?

In my C # .net winform application, the following exception appears.

A common mistake in GDI +.
in System.Drawing.Graphics.CheckErrorStatus (status Int32)
in System.Drawing.Graphics.DrawRectangle (Pen, Int32 x, Int32 y, Int32 width, Int32 height)
in WeifenLuo.WinFormsUI.Docking.DockWindow.OnPaint (PaintEventArgs e)
in System.Windows.Forms.Control.PaintWithErrorHandling (PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
in System.Windows.Forms.Control.WmPaint (Message & m)
in System.Windows.Forms.Control.WndProc (Message & m)
in System.Windows.Forms.ScrollableControl.WndProc (Message & m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage (Message & m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc (Message & m)
in System.Windows.Forms.NativeWindow.Callback (IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The most confusing point is that it is very rare when an application loading a form (which contains some rich graphics, WPF work, etc.) can tell about 90% of the time when the form is loaded successfully. but very few times when this happens is reproduced, and also only on some machines, on some other machines, this form works in 100% of cases and never encounters this exception.

I don’t understand why this is happening, because it also does not show the exact stack trace that throws an exception. Please suggest if you have an idea of ​​how to deal with this.

+4
source share
1 answer

Your code is probably heavily polluting GDI resources. Take a look at the tab Taskmgr.exe, Processes. View + Select Columns and mark descriptors, USER objects, and GDI objects. Run the program and see the displayed values ​​for your process. The ever-rising value for GDI objects causes problems; the show ends when it reaches 10,000.

What can cause a leak is not so easy to diagnose. Although you can take one step of your code in the debugger and keep track of the taskmgr number. A classic mistake is creating pens and brushes in the Paint event handler, rather than deleting them. Without a garbage collector working often enough to clean. Use the using statement to fix it.

+7
source

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


All Articles