Microsoft KB Archive/65177

= &quot;Out of Stack Space&quot; Using RETURN for SUB Event =

Article ID: 65177

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS

-



This article was previously published under Q65177



SUMMARY
If an event occurs in a procedure (SUB or FUNCTION), then returning from event-handling with the RETURN statement always leaves unrecoverable information on the stack, which can lead to the error message &quot;Out of Stack Space&quot; after many trapped events.

This behavior is a result of violating the following design rule: to correctly restore (pop) the stack after handling an event, you must always return to the procedure level where the event occurred. This applies to all events trapped with the ON GOSUB statement (where includes COM, KEY, PEN, PLAY, TIMER, STRIG, and others).

RETURN or is only designed to return from events that occur at the module-level (main-level) code. This correctly pops the stack.

You must use RETURN without the or option if you want to RETURN to a SUB or FUNCTION procedure where an event was trapped. This correctly pops the stack.

This information applies to Microsoft Basic Professional Development System (PDS) versions 7.00 and 7.10 for MS-DOS and MS OS/2; to Microsoft Basic Compiler versions 6.00 and 6.00b for MS-DOS and MS OS/2; and to Microsoft QuickBasic versions 1.00, 1.01, 1.02, 2.00, 2.01, 3.00, 4.00, 4.00b, and 4.50 for MS-DOS.



MORE INFORMATION
To demonstrate the &quot;Out of stack space&quot; message, run the following program and hold down the ESC key, which will be trapped in a loop until the error occurs. The &quot;Out of stack space&quot; error occurs because this program incorrectly allows events in the SUB to be handled by the RETURN instead of an ordinary RETURN.

Code Example
DECLARE SUB test ' This is an example where the RETURN ' statement gives you &quot;Out of Stack Space&quot; (after about 53 ESC key ' trap iterations in QBX.EXE, or 118 if compiled in BC.EXE) when the ' event (pressing the ESC key) is trapped in a SUB procedure. KEY 15, CHR$(0) + CHR$(1)   ' Trap ESC key ON KEY(15) GOSUB escape KEY(15) ON PRINT &quot;now in main&quot; again: CALL test PRINT &quot;Done&quot; END escape: j = j + 1 ' The FRE(-2) function returns a value decreased at each iteration by ' the number of bytes of stack (associated with the SUBprogram) that ' were lost: PRINT j; &quot;ESC key was pressed. Continue in main. FRE(-2)=&quot;; FRE(-2) KEY(15) ON  ' <-- Must say KEY(15) ON here or else the '  RETURN statement will leave the '  ON KEY(15) GOSUB trap still active, which does an               '   implied KEY(15) STOP. If the key had been trapped '  in the main program, then RETURN would '  work normally, and you wouldn't have to use '  KEY(15) ON here. RETURN again

SUB test STATIC PRINT &quot;Now in SUB&quot; WHILE INKEY$ = &quot;&quot;: WEND PRINT &quot;You pressed some key other than ESC.&quot; END SUB

