Microsoft KB Archive/103461

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

Article ID: 103461

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft Visual Basic 1.0 Standard Edition
 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic for MS-DOS

-



This article was previously published under Q103461



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 query words: 1.00 2.00 3.00 B_VBMSDOS

Keywords: kbprb KB103461

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.