Partager via


Visual Basic Concepts

Monitoring the Call Stack

The Call Stack dialog box shows a list of all active procedure calls. Active procedure calls are the procedures in the application that were started but not completed.

The Call Stack dialog box helps you trace the operation of an application as it executes a series of nested procedures. For example, an event procedure can call a second procedure, which can call a third procedure — all before the event procedure that started this chain is completed. Such nested procedure calls can be difficult to follow and can complicate the debugging process. Figure 13.17 shows the Call Stack dialog box.

Note   If you put the application in break mode during an idle loop, no entries appear in the Call Stack dialog box.

Figure 13.17   The Call Stack dialog box

You can display the Call Stack dialog box only when the application is in break mode.

To display the Call Stack dialog box

  • From the View menu, choose Call Stack.

    -or-

    Click the Call Stack button on the Debug toolbar. (To display the Debug toolbar, right-click on the Visual Basic toolbar and select the Debug option.)

    -or-

    Press CTRL+L.

    -or-

    Click the button next to the Procedure box in the Locals window.

Tracing Nested Procedures

The Call Stack dialog box lists all the active procedure calls in a series of nested calls. It places the earliest active procedure call at the bottom of the list and adds subsequent procedure calls to the top of the list.

The information given for each procedure begins with the module or form name, followed by the name of the called procedure. Because the Call Stack dialog box doesn't indicate the variable assigned to an instance of a form, it does not distinguish between multiple instances of forms or classes. For more information on multiple instances of a form, see "Programming with Objects" and "Multiple-Document Interface (MDI) Applications" in "Designing a User Interface."

You can use the Call Stack dialog box to display the statement in a procedure that passes control of the application to the next procedure in the list.

To display the statement that calls another procedure in the Calls Stack dialog box

  1. In the CallStack dialog box, select the procedure call you want to display.

  2. Choose the Show button.

    The dialog box is closed and the procedure appears in the Code window.

The cursor location in the Code window indicates the statement that calls the next procedure in the Call Stack dialog box. If you choose the current procedure in the Call Stack dialog box, the cursor appears at the current statement.

Checking Recursive Procedures

The Call Stack dialog box can be useful in determining whether "Out of stack space" errors are caused by recursion. Recursion is the ability of a routine to call itself. You can test this by adding the following code to a form in a new project:

Sub Main()
   Static intX As Integer
   intX = intX + 1
   Main
End Sub

Private Sub Form_Click()
   Main
End Sub

Run the application, click the form, and wait for the "Out of stack space" error message. Choose the Debug button, and then choose Call Stack on the View menu. You'll see multiple calls to the Main procedure, as shown in Figure 13.18.

Figure 13.18   The Call Stack dialog box lists a recursive procedure

As a double check, highlight intX in the Code window, and choose Quick Watch from the Debug menu. The value for intX is the number of times the Main procedure executed before the break.