Skip to content

Advanced record as member of an oldstyle object not initialized properly

Summary

In the code below, console output shows the record LocalGEN being initialised upon program start. Then, Sys is initialised, and the address of its own Sys.LocalGEN is the same as reported during LocalGen's initalisation. But, Sys.LocalGen.state is 0 instead of 55.

System Information

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

Steps to reproduce

compile and run

Example Project

{$APPTYPE CONSOLE}
{$IFDEF FPC}
  {$MODE OBJFPC}
  {$MODESWITCH ADVANCEDRECORDS}
{$ENDIF}

type  TGen= record
        state: longword;
        class operator Initialize (var R: TGen);
      end;

      TSys = object //issue is solved for TSys as Class but not for objects
        LocalGen: TGen;
        constructor Init;
      end;

(********* TGen *****************************************)
class operator TGen.Initialize (var R: TGen);
begin
  R.state := 55;
  writeln ('TGEN initialized ! My address is ',ptruint (@R));
end;

(********* TSYS *******************************************)
constructor TSys.Init;
var i: integer;
begin
  inherited;
  writeln ('Sys ok');
//Initialize (LocalGEN); //this solve it but then LocalGEN is initialized twice
end;

var Sys: TSys;
begin
  writeln ('About initialising Sys');
  Sys.init;
  writeln ('The address of Sys.LocalGEN is ',ptruint (@Sys.LocalGEN));
  writeln ('Sys.LocalGEN.state is: ', Sys.LocalGEN.state);
  writeln;
end.

What is the current bug behavior?

Sys.LocalGen.state returns 0 instead of 55

Possible fixes

This was fixed for Classes as record owner in 27c1bb3b. But with Objects the issue returns.

Attachment

testadvrec2.pas

To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information