tags:

views:

40

answers:

4

Hi

Exception type: System.ComponentModel.Win32Exception
Exception message: Error creating window handle.

I could not reproduce it in development environ, although from time to time it appears at the end user. I know this may be a little too general and vague, but if you encountered such an exception what were the potential causes?

A: 

From what I can see, this is an exception when your error handling dialog is trying to be displayed.

leppie
A: 

Possibly, the application has a lot of forms being opened by user but not necessarily closed and disposed properly. Things like these are pretty hard to reproduce because developers and QA folks usually never use the app like the end user.

SKG
+1  A: 

There are a couple of possibilities for this exception. But if it is intermittent then there's really only one. Your code is leaking window handles. A program can allocate up to 10,000 of them before Windows pulls the plug and refuses to allow it to create any more.

Hard to repro because leaks like this take a while to build up to the quota. But easily observed from Taskmgr.exe. Click the Processes tab, View + Select columns and tick "USER objects". Observe this number while you run your program. It will go up by one every time your app creates a new form or control. And should go down when you close a form.

Unfortunately it is fairly easy to make a mistake that causes such a leak. It happens when you remove a control from a form without calling its Dispose() method. Or calling Controls.Clear() without disposing the controls first. Controls that are removed like that are temporarily hosted on the "parking window". With the intent that it will survive long enough to allow you to move them to another container. If that doesn't happen then the window handle for the control is permanently leaked.

Hans Passant