Section 7.8. Method Call Stack and Activation Records


7.8. Method Call Stack and Activation Records

To understand how Visual Basic performs method calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. You can think of a stack as analogous to a pile of dishes. When a dish is placed on the pile, it is normally placed at the top (referred to as pushing the dish onto the stack). Similarly, when a dish is removed from the pile, it is normally removed from the top (referred to as popping the dish off the stack). Stacks are known as last-in, first-out (LIFO) data structuresthe last item pushed (inserted) onto the stack is the first item popped (removed) from the stack.

When a program calls a method, the called method must know how to return to the correct location in its caller, so the return address in the calling method is pushed onto the method call stack. If a series of nested method calls occurs (e.g., method A calls method B, and method B calls method C), the successive return addresses are pushed onto the stack in last-in, first-out order so that each method can return to its caller.

The method call stack also contains the memory for the local variables used in each invocation of a method during a program's execution. A method's local variables also include its parameters. This data, stored as a portion of the method call stack, is known as the activation record or stack frame of the method call. When a method call is made, the activation record for that method call is pushed onto the method call stack. When the method returns to its caller, the activation record for the returning method call is popped off the stack and those local variables are no longer known to the program. If a local variable holding a reference to an object is the only variable in the program with a reference to that object, when the activation record containing that local variable is popped off the stack, the object can no longer be accessed by the program and it will eventually be deleted from memory by the CLR during garbage collection. We discuss garbage collection in Section 9.9.

Of course, the amount of memory in a computer is finite, so only a certain amount of memory can be used to store activation records on the method call stack. If more method calls occur than can have their activation records stored on the method call stack, an error known as a stack overflow occurs.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net