Recent

Author Topic: Bug: Advanced records as members of a static array are not initialised  (Read 411 times)

Nitorami

  • Hero Member
  • *****
  • Posts: 598
FPC seems to have problems with managed records within static structures. I reported a similar issue two years ago, advanced records within old style objects not being initialised, https://gitlab.com/freepascal.org/fpc/source/-/issues/40199.

Now I experienced the same for advanced records as members of a global static array. They are finalised, but not initialised. Can someone please check and confirm, then I'll issue a bug report. Tested with fpc 3.2.2. and 3.2.4-rc1 for 32 bit under windows 10.

Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3. {$MODESWITCH ADVANCEDRECORDS}
  4.  
  5. type  TFoo= record
  6.         state: word;
  7.         procedure SetState (n: word);
  8.         class operator Initialize (var R: TFoo);
  9.         class operator Finalize (var R: TFoo);
  10.       end;
  11.  
  12. class operator TFoo.Initialize (var R: TFoo);
  13. begin
  14.   R.state := 0;
  15.   writeln ('TFoo initialized at address ',ptruint (@R.state));
  16. end;
  17.  
  18. class operator TFoo.Finalize (var R: TFoo);
  19. begin
  20.   writeln ('TFoo finalized, address ',ptruint (@R.state));
  21. end;
  22.  
  23. procedure TFoo.SetState (n: word);
  24. begin
  25.   state := n;
  26. end;
  27.  
  28. var Foo1: TFoo;
  29.     AllFoo: array[0..5] of TFoo;
  30.     i: integer;
  31.  
  32. begin
  33.   Foo1.SetState(i);  //Foo1 is initialised and finalised correctly
  34.   for i := 0 to 5 do AllFoo[i].SetState(i); //these are only finalised but never initialised !!!!
  35. end.
  36.  

Zvoni

  • Hero Member
  • *****
  • Posts: 3140
Re: Bug: Advanced records as members of a static array are not initialised
« Reply #1 on: October 09, 2025, 02:51:11 pm »
Cannot confirm.
FPC/Lazarus Fixes 64-Bit on Win10 64Bit
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$APPTYPE CONSOLE}
  3. {$MODE OBJFPC}
  4. {$MODESWITCH ADVANCEDRECORDS}
  5.  
  6. type  TFoo= record
  7.         state: word;
  8.         procedure SetState (n: word);
  9.         class operator Initialize (var R: TFoo);
  10.         class operator Finalize (var R: TFoo);
  11.       end;
  12.  
  13. class operator TFoo.Initialize (var R: TFoo);
  14. begin
  15.   R.state := 0;
  16.   writeln ('TFoo initialized at address ',ptruint (@R.state));
  17. end;
  18.  
  19. class operator TFoo.Finalize (var R: TFoo);
  20. begin
  21.   writeln ('TFoo finalized, address ',ptruint (@R.state));
  22. end;
  23.  
  24. procedure TFoo.SetState (n: word);
  25. begin
  26.   state := n;
  27. end;
  28.  
  29. Procedure TestMe;
  30. var Foo1: TFoo;
  31.     AllFoo: array[0..5] of TFoo;
  32.     i: integer;
  33. begin
  34.   Foo1.SetState(i);  
  35.   for i := 0 to 5 do AllFoo[i].SetState(i);
  36. end;
  37.  
  38. Begin
  39.   TestMe;
  40.   Readln;
  41. End.

Returns
Quote
TFoo initialized at address 20971052
TFoo initialized at address 20971040
TFoo initialized at address 20971042
TFoo initialized at address 20971044
TFoo initialized at address 20971046
TFoo initialized at address 20971048
TFoo initialized at address 20971050
TFoo finalized, address 20971052
TFoo finalized, address 20971040
TFoo finalized, address 20971042
TFoo finalized, address 20971044
TFoo finalized, address 20971046
TFoo finalized, address 20971048
TFoo finalized, address 20971050
7 x Initialize, 7 x Finalize
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 18376
  • Here stood a man who saw the Elbe and jumped it.
Re: Bug: Advanced records as members of a static array are not initialised
« Reply #2 on: October 09, 2025, 03:18:24 pm »
I can reproduce it in trunk from today win11/64 and Ubuntu 24/64
Code: Text  [Select][+][-]
  1. TFoo initialized at address 4416784
  2. TFoo finalized, address 4416784
  3. TFoo finalized, address 4416800
  4. TFoo finalized, address 4416802
  5. TFoo finalized, address 4416804
  6. TFoo finalized, address 4416806
  7. TFoo finalized, address 4416808
  8. TFoo finalized, address 4416810

(This may well be the cause that one of my own projects failed too)
« Last Edit: October 09, 2025, 03:23:22 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Nitorami

  • Hero Member
  • *****
  • Posts: 598
Re: Bug: Advanced records as members of a static array are not initialised
« Reply #3 on: October 09, 2025, 03:22:06 pm »
Thank you. May we conclude that this is fixed in the fixes branch but not in trunk ? Shall I issue a bug report then ?

O wait - Zvoni, you have modified the test program and packed the array into a procedure. That works correctly, but NOT if the array is global. Please repeat test with the original code.
« Last Edit: October 09, 2025, 03:25:17 pm by Nitorami »

Thaddy

  • Hero Member
  • *****
  • Posts: 18376
  • Here stood a man who saw the Elbe and jumped it.
Re: Bug: Advanced records as members of a static array are not initialised
« Reply #4 on: October 09, 2025, 03:23:56 pm »
This is certainly not fixed in fixes. Just tested.(not RC1, fixes)
Also: globals also need initialization so it is definitely a bug (for global containers like a fixed array)
And the bug is platform and OS independent . Same on AARCH64/linux
« Last Edit: October 09, 2025, 04:35:04 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Thaddy

  • Hero Member
  • *****
  • Posts: 18376
  • Here stood a man who saw the Elbe and jumped it.
Re: Bug: Advanced records as members of a static array are not initialised
« Reply #5 on: October 09, 2025, 04:42:31 pm »
Wait with the report: there is a commit in trunk from 4 hours ago that is related.
Now building.

Related, but did not help.

What did help: the compiler warns that allfoo is not initialized.
When I did that manually:   initialize(allfoo); it works, but should not be necessary.
« Last Edit: October 09, 2025, 04:50:09 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018