Unhandled exception Win32Exception,Error creating window handle

————————————
Unhandled exception Win32Exception,Error creating window handle.,System.Windows.Forms, at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.CreateGraphicsInternal()
at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
at System.Windows.Forms.ThreadContext.OnThreadException(Exception t)
at System.Windows.Forms.Control.WndProcException(Exception e)
at System.Windows.Forms.ControlNativeWindow.OnThreadException(Exception e)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.ComponentManager. System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager. FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
————————————————

This is a very boring error, it means that all the available windows handlers are finished so your application can’t create new windows. The limit seems to be 1000 windows but as you can imagine it’s really hard to think that someone could write a program that uses 1000 opened windows. The problem is that somewhere in your code you think you have closed forms and released controls but they’re just hidden and continue to occupy memory and handlers. I had this problem with a software developed by my company, RYHAB Solutions, and we lost a lot of time to figure out where the problem was. At the end we understood what was going on.

We had in our code something like this:

public sub CloseWindow()
    if typeof(me) is mybaseclass then
          me.visible=false
    else
          me.close()
    end if
end sub

We used this sub in a base class so all the classes that hinerited by this returned “mybaseclass” when calling typeof(me). This caused all the windows to be hidden instead of closed so they occupied all the available handlers in few hours and program crashed.

I searched a lot on the web and a lot of people talked about freeing manually resources and other strange things, someone assumed that it could be a .net bug. Actually i can say that it was my fault, no strange things behind, just a programming bug. I’m sure that most of the people that have the same problem could solve it as i did just ensuring that they close their forms properly.
Don’t waste time searching for strange solutions, just check what you do with forms :)

If you have this error message in your application use the CLR Debugger provided by Microsoft. It’s a graphical tool that shows you everything appening in a managed application, opened forms, memory usage, controls, events … everything. This is really useful to understand if something you tought was closed is opened instead.

Here is the link to download the tool:
http://msdn2.microsoft.com/en-us/library/7zxbks7z.aspx

maybe it requires the Platform SDK, you can get it on Microsoft site too.



Leave a Reply