Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.ComponentModel.Win32Exception: The operation completed successfully

I am getting this exception sometimes while running my Windows Forms app for a long time:

System.ComponentModel.Win32Exception: The operation completed successfully
   at System.Drawing.BufferedGraphicsContext.CreateCompatibleDIB(IntPtr hdc, IntPtr hpal, Int32 ulWidth, Int32 ulHeight, IntPtr& ppvBits)
   at System.Drawing.BufferedGraphicsContext.CreateBuffer(IntPtr src, Int32 offsetX, Int32 offsetY, Int32 width, Int32 height)
   at System.Drawing.BufferedGraphicsContext.AllocBuffer(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
   at System.Drawing.BufferedGraphicsContext.AllocBufferInTempManager(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle)
   at System.Drawing.BufferedGraphicsContext.Allocate(IntPtr targetDC, Rectangle targetRectangle)
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.DataGridView.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)

What could be the cause for this?

like image 923
Jakub Kaleta Avatar asked Jul 30 '09 22:07

Jakub Kaleta


2 Answers

Just to sum it up, the custom grid I wrote, that is based on the .Net's DataGridView, uses custom code to draw cells. Rows in my grid can span multiple visual pages. (That was a business requirement)

The problem was that .Net pre-allocates a buffer of memory for controls with DoubleBuffering enabled. For DataGridViews grids the buffer needs to be rather large to accommodate possible large rows in the grid. In extreme cases a row can span up to 32000 pixels (because of a .net limitation). Grid widths in the project are usually between 500 and 800 pixels. So the resulting buffer can be (32bpp * 800 * 32000 = ~100MB)

So in short, the system could not create compatible graphics objects, because occasionally, it could not reserve a buffer large enough to fit the required data.

To fix it I had to introduce a series of optimizations:

  • limited max row height allowed in my custom grid to 1500 pixels
  • updated buffer re-allocation code to only execute when the new buffer size is greater than the existing
  • ensured that the buffers are not reallocated with every data binding, and preallocated to a sensible size.
  • reviewed all code and made sure that unmanaged resources are properly disposed when not in use, as recommended here: http://nomagichere.blogspot.com/2008/03/systemcomponentmodelwin32exception-is.html
like image 56
Jakub Kaleta Avatar answered Oct 08 '22 05:10

Jakub Kaleta


Windows has a hard limit of 10000 handles per process. The rather unhelpful exception "The operation completed successfully" might indicate that this limit was reached.

If this happened because of a resource leak in your code, then you are in luck, as you at least have the opportunity to fix your code.

Unfortunately, there is scarcely little you can do about handles created internally by WinForms. For example, the prolific creation of font handles by TreeView control makes it hard to use in a scenario where very large tree needs to be represented in UI.

Some useful links:

http://support.microsoft.com/kb/327699 http://nomagichere.blogspot.com/2008/03/systemcomponentmodelwin32exception-is.html

like image 18
Branko Dimitrijevic Avatar answered Oct 08 '22 05:10

Branko Dimitrijevic