Re-compiling with -O- (rather than -01) fixed the error !!
then it might be the "peephole optimizer" (you can disable that by option or directive).
I have no idea what could create such an issue but I would appreciate some explanation as to why I should - or should not - use optimization? I cannot recall ever changing that setting.
If it is at all relevant I can confirm that I'm using Laz 2.2.0.
This is more about the FPC version. But even 3.2.2 has some issues with that.
There are 2 general areas:
1) not affecting your code, just what the debugger displays.
Normally if a variable is computed, that happens in a register, which is then stored to mem. If the next line of code uses the same value then that may write code like
- store res to mem
- read mem to res (for next line)
the 2nd obviously is not needed (if it is the same reg) => but when fpc optimized that it would mix up line numbers for the debugger (not sure that may be fixed in 3.2.2). So the debugger would not see the value having been stored to mem, and show the old mem content. However your program would run correctly.
2) Actual code generation bugs. Unfortunately there are one or two.
IIRC under some condition, if a value (as in the above) is still in a register (and a copy is hold there, while other lines are executed), the compiler would forget, and use the register incorrectly => so the generated exe is broken.
The latter, that 2nd happens really rare. But it can happen.
- As far as I know they are fixed in 3.2.3
- there is a known issue with inlining going wrong, and that still happens in 3.3.1 (happens when inlined code contains other inlined code, and a couple of other conditions are true at the same time , search the bug tracker for inline)
For Windows there are builds of 4.6 and 4.8 with 3.2.4-branch (same as 3.2.3 for all practical purposes)
https://sourceforge.net/projects/lazarus-snapshots/files/It may be possible to point an older Lazarus to use that newer compiler....