Tuesday, November 17, 2015

c# windows application, Error creating window handle (Win32 Exception) or red square with X across


If you are developing a windows application and you get an error like this one:

Application: Tool.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.OutOfMemoryException Stack: at System.Drawing.Graphics.FromHdcInternal(IntPtr) at System.Drawing.Font.GetHeight() at System.Drawing.Font.get_Height() at System.Windows.Forms.Control.get_FontHeight() at System.Windows.Forms.TextBoxBase.get_PreferredHeight() at System.Windows.Forms.TextBoxBase.get_DefaultSize() at System.Windows.Forms.Control..ctor(Boolean) at System.Windows.Forms.TextBoxBase..ctor() at System.Windows.Forms.TextBox..ctor() at System.Windows.Forms.ThreadExceptionDialog..ctor(System.Exception) at System.Windows.Forms.Application+ThreadContext.OnThreadException(System.Exception) at System.Windows.Forms.Control.WndProcException(System.Exception) at System.Windows.Forms.Control+ControlNativeWindow.OnThreadException(System.Exception) at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr) 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 System.Windows.Forms.Application.Run(System.Windows.Forms.Form) at Tool.Program.Main() 

It is because there is a leak of memory in your program, some objects are not released, usually is because you are building controls dynamically.
This program will generate others like a red square with an X inside:


To identify what is the root of the problem do this:

-Run Process Explorer or the Windows Task Manager to look at the GDI Objects, Handlers, Threads and USERS objects, run your application and see which column is growing really large.
The windows handle limit for your application is 10,000 handles, you can increase this value in the registry but is not the best solution:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\USERProcessHandleQuota

The other limit you have is 66,536 user handles per Windows session and 11,000 for GDI Objects depending on the system.

- Use the application Bear, it shows resource usage of all GDI objects & user objects, and handle count, it is a great tool to identify memory leaks.




- Use the Desktop Heap Monitor to see your limits

-On Visual Studio break all the exceptions (Control/Alt + E) then check the Common Language Run-time Exceptions, in order to get all the exceptions


Solutions:
-Dispose you objects manually, here you have a good article about it.
-Reuse components specially  the GDI Objects; for example if you are creating dynamically objects like ToolTip, they stay in memory, they don't get disposed.


No comments:

Post a Comment