none
[Out of Memory] Error creating window handle

    Question

  •  

    Hi All,

    My application was encouted the following exception.

    I don't have any idea to find out the root cause. Beacause it only happens in Maximum window.

    Could any one provide some idea or some tool for me to debug it?

    Thanks a lot.

     

    Pogo

     

    ************** Exception Text **************
    System.OutOfMemoryException: Error creating window handle. ---> System.NullReferenceException: Object reference not set to an instance of an object.
       at System.Windows.Forms.NativeWindow.WindowClass.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
       at System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
       at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
       --- End of inner exception stack trace ---
       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.Show()
       at KT.ACE.GUI.IM_OutputDisplayManager.showOutputResult(TreeNode RelatedNodeResultTreeNode)
       at KT.ACE.GUI.IM_OutputDisplayManager.ShowOutputResultAfterIMNodeGuiContainerClosed(IMNodeGuiContainer ClosingIMNodeGuiContainer)
       at KT.ACE.GUI.IM_OutputDisplayManager.IMNodeGUIContainer_Closed(Object sender, EventArgs e)
       at System.Windows.Forms.Form.OnClosed(EventArgs e)
       at System.Windows.Forms.Form.WmClose(Message& m)
       at System.Windows.Forms.Form.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)

     

    Monday, March 24, 2008 7:06 AM

Answers

  • So if I understand you correctly you have a main form.  When you do something in the main form it will create a new child form (TabbedResultForm).  You can have more than one of these child forms open at a time.  If you maximize one of the child forms and then try to create a new child form (from the parent form) it will throw the exception. 

     

    You mention that if you try to call one child form from another the problem will occur.  How are you calling the other child form?  Does this happen with only 2 child forms or do you have to have more open to cause the problem?

     

    I'm strictly looking at the inner exception here.  The outer exception is just a generic exception that the code eventually returns in this special case.  When you call your ShowOutputResult method it looks like you are calling Show on the form.  However this doesn't actually set up the parent-child form relationship.  Instead it just shows the form.  Therefore your child forms are really top-level forms with your main form.  You should call the overloaded version that accepts the parent as a parameter to set up the parent-child relationship.  I doubt that it would impact the exception you're getting but it might.

     

    Within the bowels of .NET when the form is shown it will attempt to create the underlying Win32 window.  It is within here that the code is failing.  Try calling Marshal.GetLastWin32Error() when the exception occurs and see what the underlying Win32 is. This might provide a better clue as to what is going on. 

     

    Michael Taylor - 3/27/08

    http://p3net.mvps.org

     

    Thursday, March 27, 2008 1:10 PM

All replies

  • Use Process Explorer (http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx) to examine your process.  Is it getting close to the 2GB boundary?  If not are your GDI/USER objects really high? 

     

    If you're running out of memory then you have a memory leak.  If you are running out of GDI/USER objects then you are leaking resources.  Resources would include images, pens, brushes and Graphics objects.  All these need to be wrapped in using statements.

     

    Without seeing the code you're using that is the best I can come up with.

     

    Michael Taylor - 3/24/08

    http://p3net.mvps.org

     

    Monday, March 24, 2008 1:19 PM
  •  

    Thanks for your reply.

     

    1. It is not getting close to the 2GB boundary.

    2. The GDI/USER object is not really high.

     

    I think it is running out of resources, but I can't find out what resources is running out. I need some solutions to debug what function exhaust the resource.

    Becacuse the same code in VS2003, this exception will not be threw out. In VS2005, I encounter many unexcepted exceptions.

     

     

    Tuesday, March 25, 2008 3:07 AM
  •  

    does your main call Application.EnableVisualStyles first?

     

    static void Main()

    {

    Application.EnableVisualStyles();

    Application.SetCompatibleTextRenderingDefault(false);

    Application.Run(new Form1());

    }

    Tuesday, March 25, 2008 4:55 AM
  • No, but I have tried to call Application.EnableVisualStyles, it still throw same exception.

    Thanks for your help.

     

     

    Tuesday, March 25, 2008 9:45 AM
  • The error is occurring inside the custom control you are calling. Do you have the source code for it?  Does the exception occurr immediately upon startup or only after a while?  Do you have the STAThread attribute applied to your main thread?

     

    Michael Taylor - 3/25/08

    http://p3net.mvps.org

     

     

    Wednesday, March 26, 2008 1:48 AM
  •  

    1.Yes, I have source code.

    2. This exception always occur when I call one object( inherit from Form)'s Show Method ( inherit from Form). It throw this exception. This object is contained as child form by AP and contains many controls. 

      2.1 When I new one object attached to AP, then I new another object attached to AP.  -> OK

      2.2 When I new one object attached to AP, but I set this object's window states as Maximize. Then I new another object attached to AP and called Show. It will throw exception I posted above.

     

    So I need some tool or solutions to find out the root case. Please help me and provide any idea. Thanks a lot.

     

     

     

    Wednesday, March 26, 2008 5:13 AM
  • The debugger is all you need to diagnose this issue.  Based upon the callstack you eventually call showOutputResult.  Within here it seems to be toggling the visibility of a form.  When this occurs the form is created and references memory. 

     

    Does the form you are instantiating (which causes the exception) work standalone (i.e. you can create it outside the custom control and it displays properly)?  If not then the problem lies in the form itself.  If it does work then is probably something related to your method call.  We'd need to see the code to figure out what is going on.  I suspect that the problem lies in your form's initialization (the one that is causing the exception).  How many controls does it contain anyway?

     

    Michael Taylor - 3/26/08

    http://p3net.mvps.org

     

    Wednesday, March 26, 2008 12:38 PM
  •  

    The callstack I eventually call is ShowOuputResult. This function will create one TabbedResultForm and call this Form to show. This TabbedResultForm is attached in one Parent Form as child form.

    If all child form( TabbedResultForm) are not maximized, then it will not encounter the exception.

    But if one child form is maximized and want to call the other form(TabbedResultForm) or create a new form(TabbedResultForm) to show, then it will throw exception.

     

    1. The TabbedResultForm can work standalone.

    2. I want to know why this form is set to maximum it will throw exception.

     

    Thanks a lot for your reply.

     

    Thursday, March 27, 2008 6:36 AM
  • So if I understand you correctly you have a main form.  When you do something in the main form it will create a new child form (TabbedResultForm).  You can have more than one of these child forms open at a time.  If you maximize one of the child forms and then try to create a new child form (from the parent form) it will throw the exception. 

     

    You mention that if you try to call one child form from another the problem will occur.  How are you calling the other child form?  Does this happen with only 2 child forms or do you have to have more open to cause the problem?

     

    I'm strictly looking at the inner exception here.  The outer exception is just a generic exception that the code eventually returns in this special case.  When you call your ShowOutputResult method it looks like you are calling Show on the form.  However this doesn't actually set up the parent-child form relationship.  Instead it just shows the form.  Therefore your child forms are really top-level forms with your main form.  You should call the overloaded version that accepts the parent as a parameter to set up the parent-child relationship.  I doubt that it would impact the exception you're getting but it might.

     

    Within the bowels of .NET when the form is shown it will attempt to create the underlying Win32 window.  It is within here that the code is failing.  Try calling Marshal.GetLastWin32Error() when the exception occurs and see what the underlying Win32 is. This might provide a better clue as to what is going on. 

     

    Michael Taylor - 3/27/08

    http://p3net.mvps.org

     

    Thursday, March 27, 2008 1:10 PM
  • 1. Your understanding is correct.

     

    2. I have one Parent Form( container) contain all result form. When I create one dialog to run some thing, then it will call showOutputResult(). This function will create TabResultForm and assign Parent Forma as its parent. Finally it will call TabResultForm.Show(). If all child form (TabResultForm) are not maximized, then it would not throw any exception. But if one child form is set as maximized, then show other child form will throw this exception.

     

    3. When I create TabResultForm, I already set Parent Form to it.

     

    4.I have tried to call Marshal.GetLastWin32Error(), but it return 127(0x7F). I can't find any useful meaning for me depend on this error code.

     

    Thanks for your help. I will tried to not set the parent to TabResultForm. And see if it still throw exception.

     

    Monday, March 31, 2008 9:54 AM
  • 3) How are you setting the parent explicitly?  Are you using Owner?  If so then be aware that calling Show/ShowDialog can (but not always) reset Owner so it isn't that useful.  Instead you should use Show and pass in the parent owner.

    4) The error message says the proc can't be found and is likely a holdover from some previous call.

     

    If you still can't figure it out is it possible for you to narrow down the code to the smallest possible subset that can replicate the problem without any database or custom control access.  We might then be able to replicate the issue.

     

    Michael Taylor - 3/31/08

    http://p3net.mvps.org

     

     

    Monday, March 31, 2008 12:42 PM
  • 3. I use the following code to set the parent.

    public TabbedResultForm(...,Form mdiParent,...)

    {

        this.MdiParent = mdiParent;

    }

     

    I will try your suggestion, setting the Parent Form when I call TabbedResultForm.Show()

     

     

    Thanks for your help, I will try to narrow down my code if I still can't figure it out.

    Thanks again.

     

     

     

    Wednesday, April 02, 2008 8:28 AM
  • Whoa, hold up a second.  That changes the entire ballgame.  Is this an MDI app?  MdiParent is only used when working with MDI apps.  More specifically the main window must be marked as an MDI container using IsMdiContainer.  Then you can assign child windows to the container via the MdiParent property.  MDI children are limited to the viewport of their parent and the parent controls the visibility of everybody.

     

    However this is not how you normally do WinForms apps.  An MDI app works quite a bit different than parent-child windows.  You don't want to be using it unless you want a true MDI app.  Instead set the owner when you call Show.  Remove all the code for setting MDI properties and try it again.

     

    Michael Taylor - 4/2/08

    http://p3net.mvps.org

     

     

    Wednesday, April 02, 2008 12:34 PM
  •  

    1. Yes, it's MDI APP. Sorry~ I forgot to remind it. I need to display many results in one window. So I choose MDI APP.

     

    2. If I remove MDI property, it works well. But it can't solve my question, I still need MDI.

     

    Thanks for your help. This app can run very well in VS2003 with MDI, but when we upgrade to VS2005 it can't work.

     

     

    Tuesday, April 08, 2008 3:00 AM
  • Unless you're opening a bunch of child windows I doubt you're running out of memory or resources.  Is it possible to simplify your code down to the bare minimal and still replicate the problem?  I tried doing what yo mentioned and I can open any number of MDI children without any issues.  I suspect there is something else getting in the way.

     

    If you can narrow down the code we can try and figure out why it is throwing an exception.  It could very well have something to do with the interaction between your tree and the parent.  Hard to say at this point.

     

    Michael Taylor - 4/8/08

    http://p3net.mvps.org

     

    Tuesday, April 08, 2008 12:50 PM