System.OutOfMemoryException Occurs When Displaying Windows Form

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

——————————————–

Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
Microsoft Corporation. All rights reserved. Terms of Use | Trademarks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image