I have tracked down an obstinate bug in my program, and isolated it in a small piece of code, see below. Can someone please confirm with FPC3.0.4, win32, and the parameters
-O2 -Op80386 -Mdelphi (or -Mobjfpc)
Procedure TMyClass.TestModes runs a loop over the 4 array elements of fOutputs and prints their length. The correct result should be
10
10
10
10
But with optimization level 2, and optimization target processor 80386, I get this
10
10
0
Runtime error 204 at $004034A0
$004034A0
$00401651
The issue is rather sensitive to small changes elsewhere in the code, and may be somewhat random. I did not manage to reproduce it with optimization off, nor when optimising for a different target processor than 80386, so I guess it is a compiler bug.
type TPoint = record x,y: integer; end;
type
TOutput = object
OName: Ansistring;
OConns : integer;
TargetList : array of TPoint;
procedure Init;
end;
TMyClass = Class
fOutputs: array [0..3] of TOutput;
procedure TestModes;
constructor create;
end;
//-------------------------------
procedure TOutput.Init;
begin
OName := '';
OConns := 0;
SetLength (TargetList,10);
end;
//-------------------------------
constructor TMyClass.Create;
var n: integer;
begin
for n := 0 to high (fOutputs) do fOutputs[n].Init;
end;
procedure TMyClass.TestModes;
var OP: TOutput;
begin
for OP in fOutputs do begin //Enumeration seems to go wrong
writeln (length(OP.Targetlist));
end;
end;
//-------------------------------
var C1: TMyClass;
begin
C1 := TMyClass.Create;
C1.TestModes;
C1.Free;
end.