Application randomly crashes after Win32Exception: Error creatingwindow handle

A

Andi

Hi,

This post is related to my previous post:
http://groups.google.com/group/micr...read/thread/aa7a7b3583685f4f/9c5c930a0396b35b

In the C# application that I'm working on, we keep getting these
random instances where it just throws a 'Win32Exception' out of
nowhere saying that it failed to create the error handle. The most
common stack trace that we get is:
System.ComponentModel.Win32Exception: Error creating window handle.
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Timer.TimerNativeWindow.EnsureHandle()
at System.Windows.Forms.Timer.TimerNativeWindow.StartTimer(Int32
interval)
at System.Windows.Forms.Timer.set_Enabled(Boolean value)
at System.Windows.Forms.MouseHoverTimer.Start(ToolStripItem item)
at System.Windows.Forms.ToolStrip.OnMouseMove(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseMove(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.MenuStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage
(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc
(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)

In the most recent occurrence of this issue, the app was just idle,
the user wasn't doing anything and suddenly it crashed with the above
exception and stacktrace.

I've done a lot of profiling and cleanup in the app, so it's properly
disposing and releasing no longer used objects. I also know that the
number of open window handles for the process was only 543 and the
memory for it was around 75MB. So I know that we didn't run out of
window handles or memory.

What else could be causing such an exception? Or what else would
explain it?

One thing that we're doing differently in this app that I haven't done
in any other app before is that when the app is launched, we don't use
the 'Application' class but rather have a class derived from
'Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase'.
That class is taking care of running the main UI thread.

Could this base class somehow be causing these issues? Would it be
better to just use 'Application.Run' instead?

Any ideas/suggestions would be great as we've just hit a wall with
this issue now.

Thanks.
Andi
 
A

Andi

I just wanted to post a follow-up to my question as we've finally
nailed down this issue.

We finally found out that the app truly was running out of window
handles. The first problem was that I wasn't checking the right
process property in the task manager. I had been looking at the
'Handles' count but instead I needed to check the 'USER Objects'
count. That count was going up and up and when it reached 10000 (which
is the default limit of user objects per process), it stayed there and
the next time the app was trying to create a window handle, it throw
the Win32Exception about error creating window handle.
We found out that every keystroke added 4 new user objects which was
very odd. We are using a low-level keyboard handler to get all
keyboard events as well as a 3rd party web browser control where we
get the user entries from. So these were the first two things I
suspected of doing something wrong but even after completely removing
those 2 things from the app, it was still happening.

In the end we narrowed it down to the .NET WebBrowser control
(Windows.Forms.WebBrowser). I created a little sample project that's a
standard Windows Forms 2.0 app with only .NET libraries references. I
added a couple of splitter panels and put the web browser control into
one of the panels and buttons in the other panels. Whenever I clicked
on a button (and notice the buttons don't do anything, none of the
events are hooked up) to get the focus into that panel, then every
keystroke would add 1 to 4 new user objects and they would keep
increasing. This only started happening once the web browser control's
'DocumentText' was initialized which you have to initialize in order
to be able to use it. It didn't matter whether we initialized it with
the document text, the stream, navigating to 'about:blank' or anything
else.

Whenever the focus was not in the panel where the browser control is,
new user objects are generated at random. I could replace the web
browser control with a text box or rich text box and nothing would
happen in this case. So there is something very strange going on with
the C# web browser control.

We've also eliminated the particular machine and environment as the
sample project/app behaved the same way on a fresh, clean WinXP
installation, on a couple of Vista machines, a few different WinXP
instances running in VM ware, etc.

Very strange this whole thing. So we've ended up replacing that web
browser control with the 3rd party web browser control as well and now
everything is working just fine. In case someone is interested in
which web browser control we're now using, it's an open source
project: csEXWB (http://code.google.com/p/csexwb2/)

Andi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top