Sum bug? lazarus / 64-FPC development ---------------------------------------------------------------------- bytebites · 2024-03-11 21:38 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program sumbug;{$mode delphi}{$ModeSwitch anonymousfunctions}{$ModeSwitch functionreferences}type { tfoo }  tr=record   x,y:integer; end;  tf=reference to procedure(i:tr);  tfoo=class  procedure repe(f:tf);end; { tfoo } procedure tfoo.repe(f: tf);var  r: tr;  i: Integer;begin  for i:=1 to 5 do begin    r.x:=i;    f(r)  end;end; procedure test;var  foo: tfoo;  c: Integer;begin  foo:=tfoo.create;  c:=0;  foo.repe(procedure (i:tr) begin    c:=c+i.x;    writeln(i.x)  end);   writeln('sum ',c);end;begin  test;end.  This code outputs sum 0 when optimization is 3 or 4. ---------------------------------------------------------------------- Thaddy · 2024-03-12 05:38 Can' t reproduce. Note you never free Tfoo so the example leaks memory. That can be the cause on higher optimization levels. Tested Windows11, x86-64 and Debian bookworm x86_64 with trunk from Sunday. Note I fixed the bug *before* testing because it was so obvious. ---------------------------------------------------------------------- cdbc · 2024-03-12 06:12 Hi With bug-fix in place, I can reproduce in -O3 & -O4 -> sum = 0. Fwiw, mode delphi or objfpc doesn't matter, neither does freeing foo... edit: Compiler compiled on january 28 2024: QuoteFree Pascal Compiler version 3.3.1-15019-g664f8fc2ba-dirty [2024/01/28] for x86_64 Copyright (c) 1993-2024 by Florian Klaempfl and othersRegards Benny ---------------------------------------------------------------------- Thaddy · 2024-03-12 06:46 Yes I can now reproduce it. I had heaptrc on. Plz report a bug. And yes 3 and 4 both have the bug, O2 is fine. ---------------------------------------------------------------------- rasberryrabbit · 2024-03-12 11:59 It's LOADMODIFYSTORE bug. Compiler switch {$Optimization NOUSELOADMODIFYSTORE} make avoid problem. I also reproduce with FPC trunk. ---------------------------------------------------------------------- Thaddy · 2024-03-12 12:52 Yes, you are correct. {$Optimization NOUSELOADMODIFYSTORE} works.