Runtime Error 202
Delphi Programming
Severity: CriticalWhat Does This Error Mean?
Runtime Error 202 means the program's call stack has been exhausted — there is no more space for new function calls. The most common cause is infinite recursion: a function that calls itself (directly or indirectly) without a proper stopping condition. Each function call consumes a small amount of stack space. Thousands of nested calls without returning will drain it entirely.
Affected Models
- Delphi 12 Athens
- Delphi 11 Alexandria
- Delphi 10.4 Sydney
- Delphi 10.3 Rio
- Delphi 10.2 Tokyo
Common Causes
- A recursive function missing a proper base case, causing it to call itself endlessly
- Two event handlers calling each other in a cycle (e.g., OnChange triggers a value change that fires OnChange again)
- A property setter that inadvertently re-triggers itself during execution
- Indirect recursion — function A calls B, B calls C, C calls A, forming an infinite loop
- Very large local variables (large arrays or records) declared inside a function, consuming stack space immediately on entry
How to Fix It
-
Run the program in the Delphi debugger. When Runtime Error 202 fires, examine the Call Stack window. If the same function appears hundreds of times, it is recursing infinitely — that is your starting point.
In Delphi IDE: View → Debug Windows → Call Stack. The pattern of repeated function names makes infinite recursion immediately obvious.
-
Add a correct base case to every recursive function. The base case is a condition that makes the function return without calling itself again. Verify that every possible execution path can reach the base case.
Example: function Factorial(N: Integer): Integer; begin if N <= 1 then Exit(1); Result := N * Factorial(N - 1); end;
-
For event handler loops (such as an OnChange handler that modifies another control which fires OnChange again), add a boolean guard flag. Declare a private FUpdating: Boolean field, set it True at the start of the handler, and exit early if already True.
Pattern: if FUpdating then Exit; FUpdating := True; try ... finally FUpdating := False; end;
-
Move large local variables off the stack. If you declare a large array or record as a local variable inside a function, allocate it on the heap instead using dynamic arrays, New(), or object constructors.
A function's stack frame is typically a few hundred bytes. A 500 KB local array immediately uses 500 KB of the 1 MB stack budget.
-
For algorithms that must handle deep structures (like tree traversal), replace recursion with an explicit stack (a TStack<T> or TList used as LIFO) combined with a while loop. This moves stack usage from the CPU call stack to the much larger heap.
An iterative approach with a manual stack can handle arbitrarily deep structures without any risk of stack overflow.
When to Call a Professional
Runtime Error 202 always has a code-level fix. Open the debugger, look at the call stack when the error fires — if you see the same function name repeated, that is your infinite recursion. For event loops, add a boolean guard flag (FUpdating) to prevent re-entry.
Frequently Asked Questions
How much stack space does a Delphi application have by default?
By default, a Delphi application has 1 MB of stack space per thread. You can increase this in Project → Options → Linker → Stack size, or using the {$MINSTACKSIZE} and {$MAXSTACKSIZE} compiler directives. However, increasing the stack size treats the symptom — fixing the infinite recursion is always the correct solution.
Why can Runtime Error 202 sometimes crash the program completely instead of showing a catchable error?
Handling an exception itself requires a small amount of stack space. If the stack is 100% exhausted, there may not be enough room left to even run the exception handler. This is why a stack overflow can sometimes produce an unrecoverable crash rather than a clean error message.
Can a new thread help avoid stack overflow in deep recursion?
You can create a new thread and specify a larger stack size for it in the constructor. This gives the recursive algorithm more room but does not fix the underlying infinite recursion problem. The correct solution is always to either add a proper base case or convert the algorithm to an iterative approach with a manual stack.