Stack

From Xojo Documentation

The stack, aka call stack, is a block of memory allocated in each process to store various data whenever a method calls another method or returns.


Return Addresses

Each method is stored at a specific location in memory. Such location is called address or pointer (which corresponds to a Ptr in Xojo). Whenever you call a method, the calling method puts its return address on top of the stack. Depending on the call and the operating system, the calling method can also put some parameters' addresses on the stack.

On the other side, the called method will (if necessary) read parameters from the stack and execute its code. When it encounters a Return statement or the end of the method, it reads from the stack the return address, i.e. the location in memory from which execution of the code should continue. Of course, that address is somewhere inside the calling method's code.


Stack Errors

There may be different errors related to a bad use of the stack which usually lead to (1) your application crashing or (2) raising a Runtime Exception.

  1. A bad value onto the stack can happen when using an invalid Ptr. It can cause different types of crash.
  2. When a method is calling itself (aka recursion), there is a limit in the number of times a method can call itself. See StackOverFlowException.


See Also