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

ID: Q103461


The information in this article applies to:


SYMPTOMS

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


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


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          : 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: June 22, 1999