Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Unhandled exceptions in System.Windows.Forms.Label.WndProc

$
0
0

Hello,

Last few days we started to seeing unhandled exceptions in our app, all stack traces contained frame System.Windows.Forms.Label.WndProc. The stack traces we saw were:

System.ArgumentException: Parameter is not valid.
   at System.Drawing.Graphics.GetHdc()
   at System.Drawing.BufferedGraphics.RenderInternal(HandleRef refTargetDC, BufferedGraphics buffer)
   at System.Drawing.BufferedGraphics.Render()
   at System.Windows.Forms.Control.WmPaint(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Label.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

or

System.OutOfMemoryException: Out of memory.
   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.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.Label.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

They didn't seem to do anything to our application nor to our GUI controls.

However today our application was terminated completely and it was because of the following crash:

Exception Info: System.AccessViolationException
Stack:
   at System.Drawing.SafeNativeMethods+Gdip.GdipDrawString(System.Runtime.InteropServices.HandleRef, System.String, Int32, System.Runtime.InteropServices.HandleRef, System.Drawing.Internal.GPRECTF ByRef, System.Runtime.InteropServices.HandleRef, System.Runtime.InteropServices.HandleRef)
   at System.Drawing.SafeNativeMethods+Gdip.GdipDrawString(System.Runtime.InteropServices.HandleRef, System.String, Int32, System.Runtime.InteropServices.HandleRef, System.Drawing.Internal.GPRECTF ByRef, System.Runtime.InteropServices.HandleRef, System.Runtime.InteropServices.HandleRef)
   at System.Drawing.Graphics.DrawString(System.String, System.Drawing.Font, System.Drawing.Brush, System.Drawing.RectangleF, System.Drawing.StringFormat)
   at System.Windows.Forms.Label.OnPaint(System.Windows.Forms.PaintEventArgs)
   at System.Windows.Forms.Control.PaintWithErrorHandling(System.Windows.Forms.PaintEventArgs, Int16)
   at System.Windows.Forms.Control.WmPaint(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.Label.WndProc(System.Windows.Forms.Message ByRef)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG ByRef)
   at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
   at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
   at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
   at [...OurApplication...].InProcessClientsManager.StartNewGuiClientInternal(System.String, System.String, System.String, System.String)
   at [...OurApplication...].InProcessClientsManager+<>c__DisplayClass3.<StartNewGuiClientAsync>b__2(System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

Some more data (more or less also applies for other - non terminating - exceptions in Label.WndProc I wrote about):
Framework Version: v4.0.30319OS: Server 2012 Standard (isolated from the internet; no updates applied for about a half a year)
# of handles at the time of crash: 2834
# of GDI objects at the time of crash: 0
Working Set/Private bytes at the time of crash: 520840K/197360K
Free memory at the time of crash: almost 30GBs

Some more background:

Our application hosts quite big number of Windows Forms (~50), hosting is happening in process (by creating the Form on new ThreadPool thread and then calling Application.Run(<reference to the form>) for each of those forms). All forms have identical code (just started with different parameters for 'business logic'). In the code we use only standard Winform controls (listview, labels, linklabels, textboxes, checkboxes, buttons and panels) - no custom controls, no unsafe, P/Invoke or similar calls and we don't recreate user controls or don't 'nullify' them (I'm explicitly mentioning this as I saw a many threads about issues in GDI which suggested that developer should explicitly dispose the graphic objects).

I reviewed all usages of Labels (as all our stack traces contains System.Windows.Forms.Label.WndProc frame), and we create all labels at the creation of forms and than we only change the Text property (invoked through Invoke method) and we don't dispose or nullify any of those.

We'll be grateful for any suggestions or pointers how to investigate or troubleshoot this further, or ideally how to prevent this from occurring.

Thanks
Jan

Btw.: I haven't yet reported this as a bug in .NET framework, but if anybody from Winform (or other) team would like to look further into this then I can provide full memory dump and potentially also affected source code. 


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>