[fpc-pascal] Coroutines and VirtualAlloc

Karoly Balogh (Charlie/SGR) charlie at scenergy.dfmk.hu
Wed Apr 19 12:37:59 CEST 2017


Hi,

On Wed, 19 Apr 2017, Ryan Joseph wrote:

> yes, I?d like to see that so I know why my example doesn?t work as I
> expected. Everything I?m hearing makes me think ?i? should keep
> incrementing after I call SetJmp and then return with JongJmp but
> there?s something I?m missing obviously.

Your example is simply broken. A few points:

First, When you're LongJmp'ing to entry state in Resume, you're expecting
the Start stack state (and with that, your "i" variable's state) to be
intact. This is not the case. After the Start method returns (stepped out)
it's stack contents are destroyed, and even get overwritten by the next
'writeln' method call, so the next time Resume is called, you get
unpredictable behavior by jumping into the stack state of Start, which no
longer exist and was overwritten since. For example, it simply crashes
with Runtime Error 216 on Mac OS X 32 bit. For you, it just uses some
"random" value on the stack (or register), which happens to be 1. I get
this even without register variables, so I doubt it has anything to do
with that... (Sorry Michael/Sven... ;)

Basically, you can only jump to a "higher" level on the stack, not equal
(from a different function) and not to a "deeper" level.

Second, if you really want to use SetJmp/LongJmp states you can use SetJmp
return value for that. If SetJmp returns 0, it wasn't returning from a
LongJmp. If it returns 1, it was returning from a LongJmp. You don't need
external state to track that.

Charlie



More information about the fpc-pascal mailing list