Winforms issue - Error creating window handle [duplicate]
Asked Answered
S

10

73

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)
Shennashensi answered 21/10, 2008 at 17:1 Comment(0)
K
54

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!

Keloid answered 21/10, 2008 at 18:6 Comment(2)
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...Coetaneous
I am researching this error in my app, however at the time the exception is thrown, my process only has 987 handles, 22 threads, 98 user objects, and 70 GDI objects. Also, that link is now dead :(Kleeman
S
32

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..

Somnus answered 21/11, 2008 at 21:38 Comment(0)
B
11

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.

Broadax answered 8/8, 2009 at 0:35 Comment(1)
No longer available: "The specified CGI application encountered an error and the server terminated the process."Ankle
F
7

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.)

Favrot answered 8/2, 2010 at 14:36 Comment(3)
Yes.. How can we solve itMadura
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.Favrot
This solve it for me "Or recreating an object needlessly when you could create it once and reference it."Bekha
H
2

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.

Homovec answered 21/10, 2008 at 17:13 Comment(0)
D
2

I got same error in my application.I am loading many controls in single page.In button click event i am clearing the controls.clearing the controls doesnot release the controls from memory.So dispose the controls from memory. I just commented controls.clear() method and include few lines of code to dispose the controls. Something like this

for each ctl as control in controlcollection

ctl.dispose()

Next

Duquette answered 1/9, 2009 at 19:10 Comment(0)
T
2

Well, in my case it was definitely the USER Objects that were out of control. I looked in the Windows Task Manager and sure enough, the USER Objects count was at 10'000 exactly.

I am dynamically embedding property and list sheets in Tab Pages by setting the Parent property of the property or list sheet's container panel to that of the Tab Page. I am conditionally recycling or re-creating the property and list sheet forms depending on the type of collection being listed or class type of the object being inspected.

NB: In Delphi, all controls had an Owner and a Parent property. Even if one changed the Parent property of a control, it would still be disposed by its owner when the owning control got destroyed.

In C# it seems that if a control e.g. a Panel is programmatically reassigned from, say, a Form to a Tab Page by changing the Panel.Parent property, calling Dispose() on the Form won't dispose the Panel, neither will calling Controls.Clear() on the Tab Page. Even a direct call Panel.Dispose() won't actually dispose it, unless its Parent is manually set to null beforehand.

Tarn answered 18/3, 2010 at 12:38 Comment(0)
F
1

I added a check that makes it work...

if (_form.Handle.ToInt32() > 0)
{
   _form.Invoke(method, args);
}

it is always true, but the form throws an error without it. BTW, my handle is around 4.9 million

Feedback answered 21/3, 2012 at 19:32 Comment(1)
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.Without
C
0

Definitely too many handles(memory leak issue):

IT Jungles: System.ComponentModel.Win32Exception: Error creating window handle

Chrysotile answered 1/6, 2010 at 2:25 Comment(0)
S
-1

The out of memory suggestion doesn't seem like a bad lead.

What is your program doing that it gets this error?

Is it creating a great many windows or controls? Does it create them programatically as opposed to at design time? If so, do you do this in a loop? Is that loop infinite? Are you consuming staggering boatloads of memory in some other way?

What happens when you watch the memory used by your application in task manager? Does it skyrocket to the moon? Or better yet, as suggested above use process monitor to dive into the details.

Selves answered 21/10, 2008 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.