Winforms issue - Error creating window handle

116,072

Solution 1

Have you run Process Explorer or the Windows Task Manager to look at the GDI Objects, Handles, Threads and USER objects? If not, select those columns to be viewed (Task Manager choose View->Select Columns... Then run your app and take a look at those columns for that app and see if one of those is growing really large.

It might be that you've got UI components that you think are cleaned up but haven't been Disposed.

Here's a link about this that might be helpful.

Good Luck!

Solution 2

The windows handle limit for your application is 10,000 handles. You're getting the error because your program is creating too many handles. You'll need to find the memory leak. As other users have suggested, use a Memory Profiler. I use the .Net Memory Profiler as well. Also, make sure you're calling the dispose method on controls if you're removing them from a form before the form closes (otherwise the controls won't dispose). You'll also have to make sure that there are no events registered with the control. I myself have the same issue, and despite what I already know, I still have some memory leaks that continue to elude me..

Solution 3

See this post of mine about "Error creating window handle" and how it relates to USER Objects and the Desktop Heap. I provide some solutions.

Solution 4

This problem is almost always related to the GDI Object count, User Object count or Handle count and usually not because of an out-of-memory condition on your machine.

When I am tracking one of these bugs, I open ProcessExplorer and watch these columns: Handles, Threads, GDI Objects, USER Objects, Private Bytes, Virtual Size and Working Set.

(In my experience, the problem is usually an object leak due to an event handler holding the object and preventing it from being disposed.)

Solution 5

I think it's normally related to the computer running out of memory so it's not able to create any more window handles. Normally windows starts to show some strange behavior at this point as well.

Share:
116,072
leora
Author by

leora

Updated on July 05, 2022

Comments

  • leora
    leora almost 2 years

    We are seeing this error in a Winform application. Can anyone help on why you would see this error, and more importantly how to fix it or avoid it from happening.

    System.ComponentModel.Win32Exception: Error creating window handle.
       at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
       at System.Windows.Forms.Control.CreateHandle()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
       at System.Windows.Forms.ButtonBase.OnVisibleChanged(EventArgs e)
    
  • Paul Williams
    Paul Williams over 10 years
    Note that he implementation of the Handle property checks to see if the handle is created, and if not, creates it. Looking in the .NET source code: public IntPtr Handle { get { ...if (!this.IsHandleCreated) { this.CreateHandle(); } ...}} The IsHandleCreated property will return you true/false without creating the handle.
  • Sebastian Contreras
    Sebastian Contreras almost 9 years
    I ran into this problem and found my app reported 9600+ USER Objects and 1800+ GDI Object. My problem is I'm not doing anything wrong, I'm just adding a lot of items to a FlowLayoutPanel. I guess I'm gonna have to 'page' the displayed data...
  • Munavvar
    Munavvar almost 5 years
    Yes.. How can we solve it
  • AlfredBr
    AlfredBr almost 5 years
    You can solve it by watching the count of Handles, Threads, GDI Objects, USER Objects, etc. while testing your program. If you see that any of those counts rise and do not fall when you perform a particular activity, look at the code that implements that activity. You should look for problems like holding on to an object long after you need it. Or creating an object repeatedly in a loop and not releasing it. Or recreating an object needlessly when you could create it once and reference it. Or hooking an event handler and later disposing the object without unhooking the even handler first.
  • programingfrik
    programingfrik over 2 years
    This solve it for me "Or recreating an object needlessly when you could create it once and reference it."