Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 949458 - Last Review: February 20, 2008 - Revision: 1.2
System.OutOfMemoryException Occurs When Displaying Windows Form
Source: Microsoft Support
RAPID PUBLISHING
RAPID PUBLISHING ARTICLES PROVIDE INFORMATION DIRECTLY FROM WITHIN THE MICROSOFT SUPPORT ORGANIZATION. THE INFORMATION CONTAINED HEREIN IS CREATED IN RESPONSE TO EMERGING OR UNIQUE TOPICS, OR IS INTENDED SUPPLEMENT OTHER KNOWLEDGE BASE INFORMATION.
Action
You have developed a Microsoft .NET Framework 2.0 Windows Forms application. The application attempts to create an instance of an MDI child form.
Result
You receive the following exception and call stack.
System.OutOfMemoryException: Error creating window handle.
   at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
   at System.Windows.Forms.Control.CreateHandle()
   at System.Windows.Forms.Form.CreateHandle()
    at System.Windows.Forms.Control.get_Handle()
    at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
   at System.Windows.Forms.Control.set_Visible(Boolean value)
    at MyApplication.MainForm.DisplayControl()
Cause
This can occur when both the following conditions are true.
1. The MDI child form contains a control which parents other controls.
2. The parent control on the MDI child form removes a child control from its Controls collection in the event handler for the Layout or Resize events.
Resolution
Do not add or remove controls from the Controls collection during the Layout or Resize events.
More Information
The System.OutOfMemoryException is not due to an out of memory condition. The problem occurs due to a System.NullReferenceException which occurred when the window's callback function executed, and referenced a null member of an internal type which tracks the native window for the control on the MDI form. The System.NullReferenceException bubbles back up into System.Windows.Forms.dll and is caught. The Windows Forms run-time assumes the window creation failed due to an out of memory condition, and then throws a System.OutOfMemoryException.
When removing controls from the Controls collection of a Parent control that contains other controls, the native windows for the child controls are temporarily reparented to an internal window (called the "Parking" window), while the Parent control is destroyed and recreated. Then the native windows for the child controls are reparented to the new native window associated with the Parent control. If the code removes controls during the Layout/Resize events, then a new native window will be created for the Parent control, but the wndproc associated with the original native window may still be valid. If the wndproc for the original native window is called; it results in the System.NullReferenceException.
DISCLAIMER
MICROSOFT AND/OR ITS SUPPLIERS MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY, RELIABILITY OR ACCURACY OF THE INFORMATION CONTAINED IN THE DOCUMENTS AND RELATED GRAPHICS PUBLISHED ON THIS WEBSITE (THE “MATERIALSâ€) FOR ANY PURPOSE. THE MATERIALS MAY INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS AND MAY BE REVISED AT ANY TIME WITHOUT NOTICE.
TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, MICROSOFT AND/OR ITS SUPPLIERS DISCLAIM AND EXCLUDE ALL REPRESENTATIONS, WARRANTIES, AND CONDITIONS WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT LIMITED TO REPRESENTATIONS, WARRANTIES, OR CONDITIONS OF TITLE, NON INFRINGEMENT, SATISFACTORY CONDITION OR QUALITY, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WITH RESPECT TO THE MATERIALS.
APPLIES TO
- Microsoft .NET Framework 2.0
- Microsoft .NET Framework 2.0 x64 Edition
| kbnomt kbrapidpub KB949458 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate
Be the first to leave feedback, to help others about this knowledge base
article.
(Optional) Name
(Optional)
Public URL Or Email
Comments
No
HTML -- Text Only Please