PRB: Out of Stack Space When One Modal Form Shows Another

Last reviewed: June 21, 1995
Article ID: Q103461
The information in this article applies to:
  • The Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 1.0, 2.0, and 3.0
  • The Standard and Professional Editions of Microsoft Visual Basic for MS-DOS, version 1.0

SYMPTOMS

Any of the following error messages can occur when two or more forms in a program repeatedly show each other modally (SHOW 1).

  • Out of stack space.
  • Out of memory.
  • Out of overlay stack space.

CAUSE

This can happen even if you unload the form, which in turn shows the next form. A form is not actually unloaded by the Unload statement until all its event procedures return (End Sub or Exit Sub). Showing a form modally suspends execution and, like a procedure call, maintains information on the stack. Further explanation is given in the MORE INFORMATION section below.

WORKAROUNDS

  • Show the forms non-modally (SHOW 0). It is acceptable practice to have forms show each other non-modally.
  • Do not have modal forms call each other continually. Instead, have an initial form call all the other forms. Think of this initial form (probably your startup form) as your foundation with all other forms called from the foundation.

MORE INFORMATION

The following example gives an Out of stack space error message. Remove the apostrophe from (uncomment) the MsgBox statements in Visual Basic for MS-DOS to see the amount of remaining stack space.

' Form1:
Sub Form_Click ()
   ' MsgBox STR$(FRE(-2))
   Unload Form1
   Form2.Show 1
End Sub

' Form2:
Sub Form_Click ()
   ' MsgBox STR$(FRE(-2))
   Unload Form2
   Form1.Show 1
End Sub

When a function or a subroutine is called, the variables in the calling procedure get pushed onto the stack. This way these values are preserved. When the function or subroutine ends on an End Function, End Sub, or Exit
Sub statement, these variables get popped off the stack, and program
execution returns to the statement that follows the call. Only then are the variables once again usable.

If a subroutine or function calls another function, program execution is halted within that subroutine or function, and the stack used is not cleared up until an End Function, End Sub, or Exit Sub is encountered. This is why you should not have two subroutines repeatedly call each other with no stopping condition.

The behavior of event procedures within forms is similar to subroutines in that when a form is shown, information is pushed onto the stack, and when forms are unloaded, information is popped off the stack. Modal forms halt program execution of all other events. However, a form is not actually unloaded by the Unload statement until all of its event procedures return with an End Sub or Exit Sub. When a modal form displays a second modal form, the second modal form puts a hold on program execution, so the first modal form cannot proceed to the rest of its code, thus making it impossible to ever reach the End Sub or Exit Sub statement. This is why you should not have modal forms show each other repeatedly.


Additional reference words: 1.00 2.00 3.00 B_VBMSDOS
KBCategory: kbprg kbcode kbprb
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.