Exception "Error when creating window handle" happens to combobox on toolstrip
I'm working on a large WinForm application based on .Net 2.0/C#, there's a steadily reproducible error "Error when creating window handle" when running the application on laptop Dell E6520 (Win7 Pro SP1, 8G RAM), while it works well on desktop machines including XP, Win7, 2008).
Here's the exception stack:
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.ComboBox.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.WindowsFormsUtils.ReadOnlyControlCollection.AddInternal(Control value)
at System.Windows.Forms.ToolStripControlHost.SyncControlParent()
at System.Windows.Forms.ToolStripControlHost.OnParentChanged(ToolStrip oldParent, ToolStrip newParent)
at System.Windows.Forms.ToolStripItem.set_ParentInternal(ToolStrip value)
at System.Windows.Forms.ToolStripSplitStackLayout.LayoutHorizontal()
at System.Windows.Forms.ToolStr开发者_JAVA百科ipSplitStackLayout.LayoutCore(IArrangedElement container, LayoutEventArgs layoutEventArgs)
at System.Windows.Forms.Layout.LayoutEngine.Layout(Object container, LayoutEventArgs layoutEventArgs)
at System.Windows.Forms.Control.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.ScrollableControl.OnLayout(LayoutEventArgs levent)
at System.Windows.Forms.ToolStrip.OnLayout(LayoutEventArgs e)
at System.Windows.Forms.Control.PerformLayout(LayoutEventArgs args)
at System.Windows.Forms.Control.System.Windows.Forms.Layout.IArrangedElement.PerformLayout(IArrangedElement affectedElement, String affectedProperty)
at System.Windows.Forms.Layout.LayoutTransaction.DoLayout(IArrangedElement elementToLayout, IArrangedElement elementCausingLayout, String property)
at System.Windows.Forms.Control.OnResize(EventArgs e)
at System.Windows.Forms.Control.OnSizeChanged(EventArgs e)
at System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 width, Int32 height, Int32 clientWidth, Int32 clientHeight)
at System.Windows.Forms.Control.UpdateBounds()
at System.Windows.Forms.Control.WmWindowPosChanged(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.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)
It happens when the form is shown where inside there's a toolstrip containing comboboxes and buttons. They are later added into toolstrip dynamically after the form is constructed while kept invisible, by calling method below:
private void Init()
{
...
foreach (ToolBarItemConfig item in configuredToolbarItems)
{
ToolStripItem toolStripItem = createToolStripItem(item);
toolStrip.Items.Add(toolStripItem);
if (toolStripItem is ToolStripComboBox)
populateComboBox(toolStripItem as ToolStripComboBox, item); // populate items of ToolStripComboBox
}
...
}
Note within Init, the toolstrip is invisible, and handles of new ToolStripComboBox items are not created (found from log). I can understand the handle creation is deferred by intention because they are not actually shown yet. However at a later point, the form is finally shown and the exception comes.
I got much inspiration from this article and could fix this issue by forcing the handle creation by accessing Handle property immediately when the ToolStripComboBox items are created.
Now my understanding is ToolStrip tries to re-create the handle of comboboxes in its OnLayout when the form is shown, but at that time those handles are still not created yet, therefore nothing to destroy -> comes "Error when creating window handle". However this is just my guess as the root cause of the problem remains unclear to me, particularly I can't explain why this happens only in specific laptop but not in desktop.
Could anyone help me understand root cause of it so I can be confident similar issue won't hurt again in the future?
Thanks in advance.
had similar problem, the number of user objects (handles) grows and explodes because you pass a limit unsupported by Windows. We solved this by disposing not used hidden forms and doing some major cleanup.
In shorts, don't keep too many controls or windows/forms open or hidden and undisposed because those objects still use windows handles.
see this interesting article from "the guy who knows" ;-)
Pushing the Limits of Windows: USER and GDI Objects – Part 1
I used Task manager to find problem. First I added some columns (User Objects, GDI) to the process tab (view -> select columns). After I start my app and navigate inside forms. And I see that amount of user objects grows up and up and up. After some critical amount app crashes. This link helps me a lot http://blogs.msdn.com/b/jfoscoding/archive/2005/08/12/450835.aspx
精彩评论