I don't understand how knowing that will help you solve your problem, however ...
If an error occurs in a command, it sets an error status before returning to user code. The user code checks the error status (as it does after every command), and if it contains a non-zero value, jumps to a cleanup section in the user code.
If the user code instead includes an END statement, that END statement unconditionally jumps to the cleanup section.
The cleanup section calls the Quit routine in the core DLL (which handles the message box display of the EXIT PROMPT command. It then returns to the cleanup section, which resets the stack pointer to a known point and then returns (presumably to the .EXE code where further cleanup takes place and all DLL's and DirectX resources are torn down).
You can pretty much get most of that from the .dbm file that's produced during compilation.
Now to your problem ... I suspect that somewhere along the line you are corrupting memory, possibly by double-freeing it, or by using it sometime after freeing it.