Recent

Author Topic: Advanced Records Initialisation bug ?  (Read 5864 times)

Nitorami

  • Hero Member
  • *****
  • Posts: 604
Re: Advanced Records Initialisation bug ?
« Reply #15 on: October 24, 2022, 09:22:40 pm »
Coming back to this presumed bug - I have not filed a bug report yet because I am uncertain. Up to now I thought that an advanced record would be initialized at runtime on the first access to any of its methods, simply by checking a flag. This would introduce a bit of overhead and speed penalty as the flag would have to be checked on each and every method call, but this may just be the price for the convenience.

But then I found, rather surprisingly, that aGen is initialized properly whenever a local function somewhere within testunit.pas merely mentions one of aGen's methods. The function may never be called, its existence alone is sufficient to prompt the compiler to initialize aGen. Something like this in the unit's implementation part does the job:

Code: Pascal  [Select][+][-]
  1. procedure neverused;
  2. begin
  3.   if false then writeln (aGen.content);
  4. end;

It looks like the compiler does a static analysis at compile time to guess if aGen will be called from somwehere, and if yes, initializes it at program start within fpc_initizalizeunits() ?

Nitorami

  • Hero Member
  • *****
  • Posts: 604
Re: Advanced Records Initialisation bug ?
« Reply #16 on: March 15, 2023, 06:55:32 pm »
Sorry for re-opening this. While PascalDragon has fixed the initial issue and this has been merged to FPC3.2.3., it only works with TSys as a Class. When making TSys an oldstyle object, the bug is back.

In the code below, LocalGEN is initialised upon program start before Sys has been initialised. That should not be a problem as both are static. Indeed after initalisation of Sys, the address of Sys.LocalGEN is the same as reported during LocalGen's own initalisation. But, Sys.LocalGen.state still returns 0 instead of 55.

Tested with FPC 3.2.3 from 2023/03/05, i386-win32.

Can someone please test and confirm, then I'll file a bug report.

Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$IFDEF FPC}
  3.   {$MODE OBJFPC}
  4.   {$MODESWITCH ADVANCEDRECORDS}
  5. {$ENDIF}
  6.  
  7. type  TGen= record
  8.         fstate: longword;
  9.         class operator Initialize (var R: TGen);
  10.         property state: longword read fstate;
  11.       end;
  12.  
  13.       TSys = object //issue is solved for TSys as Class but not as object!
  14.         LocalGen: TGen;
  15.         constructor Init;
  16.       end;
  17.  
  18. (********* TGen *****************************************)
  19. class operator TGen.Initialize (var R: TGen);
  20. begin
  21.   R.fstate := 55;
  22.   writeln ('TGEN initialized ! My address is ',ptruint (@R));
  23. end;
  24.  
  25. (********* TSYS *******************************************)
  26. constructor TSys.Init;
  27. var i: integer;
  28. begin
  29.   inherited;
  30.   writeln ('ok');
  31. //  Initialize (LocalGEN); //this would solve it but then LocalGen.Init is called twice
  32. end;
  33.  
  34. var Sys: TSys;
  35. begin
  36.   writeln ('About initialising Sys');
  37.   Sys.init;
  38.   writeln ('The address of Sys.LocalGEN is ',ptruint (@Sys.LocalGEN));
  39.   writeln ('Accessing Sys.LocalGEN.state results in : ', Sys.LocalGEN.state);
  40.   writeln;
  41. end.
  42.  
  43.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 6238
  • Compiler Developer
Re: Advanced Records Initialisation bug ?
« Reply #17 on: March 15, 2023, 11:17:45 pm »
Can someone please test and confirm, then I'll file a bug report.

Please report.

MrDan

  • New member
  • *
  • Posts: 7
Re: Advanced Records Initialisation bug ?
« Reply #18 on: November 13, 2025, 03:34:14 am »
looks like this issue has still not been fixed. it is happening in the current trunk of fpc (13/11/2025)
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. {$modeswitch advancedrecords}
  3.  
  4. type TMyRec = record
  5. public
  6.   class operator Initialize(var v: TMyRec);
  7.   class operator Finalize(var v: TMyRec);
  8. end;
  9.  
  10. type TMyClass = class
  11.   var Rec: TMyRec;
  12. end;
  13.  
  14. type TMyClass2 = class (TMyClass)
  15. end;
  16.  
  17. class operator TMyRec.Initialize(var v: TMyRec);
  18. begin
  19.   WriteLn('Initialize');
  20. end;
  21.  
  22. class operator TMyRec.Finalize(var v: TMyRec);
  23. begin
  24.   WriteLn('Finalize');
  25. end;
  26.  
  27. var MyObj: TMyClass2;
  28.  
  29. begin
  30.   MyObj := TMyClass2.Create;
  31.   FreeAndNil(MyObj);
  32. end.
  33.  
the output is a single line "Finalize". initialization is not called. this only happens if the managed record is in inherited class.

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 306
  • I use FPC [main] 💪🐯💪
Re: Advanced Records Initialisation bug ?
« Reply #19 on: November 13, 2025, 04:22:47 am »
looks like this issue has still not been fixed. it is happening in the current trunk of fpc (13/11/2025)
...
the output is a single line "Finalize". initialization is not called. this only happens if the managed record is in inherited class.

I can't confirm on Windows...
FPC [main] (today) x64

UPD: And on Linux too

In the output, I see:
Code: Pascal  [Select][+][-]
  1. Initialize
  2. Finalize
« Last Edit: November 13, 2025, 04:35:50 am by ALLIGATOR »
I may seem rude - please don't take it personally

MrDan

  • New member
  • *
  • Posts: 7
Re: Advanced Records Initialisation bug ?
« Reply #20 on: November 13, 2025, 04:39:43 am »
this is very surprising. I also have tested on Windows x64 and Linux x64. I use fpcupdelux build of trunk version and initialization does not trigger on either OS. thank you for testing, I will try other versions

tested lazarus 4.4 with fpc 3.2.2 from https://www.lazarus-ide.org/ and it has the same problem. only 'Finalize' appears in the output
« Last Edit: November 13, 2025, 05:08:57 am by MrDan »

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 306
  • I use FPC [main] 💪🐯💪
Re: Advanced Records Initialisation bug ?
« Reply #21 on: November 13, 2025, 05:08:05 am »
I use fpcupdelux
Me too

Use any tool to verify that your local FPC repository is indeed updated/being updated

Personally, I use the TortoiseGit application on Windows. However, you can also do this with Git alone:
1. Go to the folder with the FPC source code.
2. Run the command git log -3 -oneline.
3. Compare the commits shown with those shown here https://gitlab.com/freepascal.org/fpc/source/-/commits/main
I may seem rude - please don't take it personally

MrDan

  • New member
  • *
  • Posts: 7
Re: Advanced Records Initialisation bug ?
« Reply #22 on: November 13, 2025, 05:39:58 am »
I verified that my git revision is at bfaa957df00f30b6b0574ff071d82f54061b0a58. yet the problem is still present

Thausand

  • Sr. Member
  • ****
  • Posts: 448
Re: Advanced Records Initialisation bug ?
« Reply #23 on: November 13, 2025, 05:48:32 am »
fwiw:
Code: [Select]
$ ./bin/fpc/trunk/fpcsrc$ git log -3 --oneline
bfaa957df0 (HEAD -> main, origin/main, origin/HEAD) Fix variable i range of values in class function TObject.GetInterfaceEntryByStr(const iidstr : shortstring) : pinterfaceentry;
ee79324b57 Fix variable i range of values in class function TObject.GetInterfaceEntry(const iid : tguid) : pinterfaceentry;
ebff68a798 Fix variable i range of values in class function TObject.InitInstance(instance : pointer) : tobject
$ ./fpc.sh -B advrecinitbug.pas
Free Pascal Compiler version 3.3.1-18840-gbfaa957df0-dirty [2025/11/12] for x86_64
Copyright (c) 1993-2025 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling advrecinitbug.pas
Linking advrecinitbug
36 lines compiled, 0.1 sec, 455248 bytes code, 198992 bytes data
$ ./advrecinitbug
Initialize
Finalize
linux/64bit/amd

MrDan

  • New member
  • *
  • Posts: 7
Re: Advanced Records Initialisation bug ?
« Reply #24 on: November 13, 2025, 05:57:18 am »
I was building the .lpr (the program file), when I moved the code into a unit the problem disappeared. so it is also specific to the code inside the program file.
moving the code into a unit is enough of a workaround for me. thank you for helping me investigate the problem.

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Advanced Records Initialisation bug ?
« Reply #25 on: November 13, 2025, 05:59:21 am »
Strange. Like ALLIGATOR tested win64 and Linux64 and the test program runs correct on both.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 306
  • I use FPC [main] 💪🐯💪
Re: Advanced Records Initialisation bug ?
« Reply #26 on: November 13, 2025, 06:00:33 am »
I verified that my git revision is at bfaa957df00f30b6b0574ff071d82f54061b0a58. yet the problem is still present

Hmm... maybe I'm doing something wrong?

I just took your code from this post:
https://forum.lazarus.freepascal.org/index.php/topic,60269.msg569808.html#msg569808

Threw it into a console application project in Lazarus, compiled it, and ran it 🤷‍♂️
I may seem rude - please don't take it personally

ALLIGATOR

  • Sr. Member
  • ****
  • Posts: 306
  • I use FPC [main] 💪🐯💪
Re: Advanced Records Initialisation bug ?
« Reply #27 on: November 13, 2025, 06:06:07 am »
I was building the .lpr (the program file), when I moved the code into a unit the problem disappeared. so it is also specific to the code inside the program file.
moving the code into a unit is enough of a workaround for me. thank you for helping me investigate the problem.

In my case, it doesn't matter where this code is located, whether in the main program file or in a unit—the result is the same, both initialization and finalization are displayed

It's really strange
I may seem rude - please don't take it personally

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Advanced Records Initialisation bug ?
« Reply #28 on: November 13, 2025, 06:07:49 am »
I compiled it from Geany with a two minutes old build from today. Opt -O2 and -O4
If you are doing something wrong I did something wrong too, but I don't think we did.
Moving code to a unit makes no difference, btw. Checked it: also the correct output.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

MrDan

  • New member
  • *
  • Posts: 7
Re: Advanced Records Initialisation bug ?
« Reply #29 on: November 13, 2025, 06:24:52 am »
I think my program cached something when I compiled it with an older version of fpc. after I moved the code to a unit and then back to program it started to work correctly.

 

TinyPortal © 2005-2018