Record strangeness... lazarus / 21-General ---------------------------------------------------------------------- JdeHaan · 2024-06-11 15:48 Hi, In a large project I have a record definition, like: 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 type PMyRec = ^TMyRec;TMyRec = record  Enclosing: PMyRec;  Name: String;  constructor Create(const aName: String);end; var  Current: PMyRec; // gets a value later on constructor TMyRec.Create(const aName: String);begin  Enclosing := Current;  Name := aName;  Current := @Self;end; In the code I initialize a variable of type TMyRec as: 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 MyRec := TMyRec.Create('ABC'); However, this didn’t work and I got access violation errors. When I changed the constructor into a normal procedure, like so: 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 procedure Init(const aName: String); procedure TMyRec.Init(const aName: String);begin  Enclosing := Current;  Name := aName;  Current := @Self;end; and used it like this: 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 MyRec.Init('ABC'); This runs without any problems. How can this be explained? Delphi mode / trunk 4 weeks ago / MacOs Sonoma 14.5 ---------------------------------------------------------------------- Thaddy · 2024-06-11 16:21 It works as expected: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 {$mode objfpc}{$modeswitch advancedrecords}{$apptype console}type PMyRec = ^TMyRec;TMyRec = record  Enclosing: PMyRec;  Name: String;  constructor Create(const aName: String);end; var  Current: PMyRec; // gets a value later on constructor TMyRec.Create(const aName: String);begin  Enclosing := Current;  Name := aName;  Current := @Self;end; var MyRec:TMyrec; begin  MyRec := TMyRec.Create('ABC');  Writeln(MyRec.Name)  Readln;//for windowsend.You probably forgot the modeswitch.... Tested on Linux64 and Windows 11. So that is not where your error comes from, it comes from mishandling the pointers to your record...,  Btw, where is init defined???? Is it the method or the procedure? I will do that for you later. I am only a human debugger, but results as close as AI.. ---------------------------------------------------------------------- JdeHaan · 2024-06-11 16:56 As mentioned I use mode Delphi, so don't need the modeswitch. The variable Current^ is used extensively in my code, so I thought maybe the assignment to @Self in the constructor code was the problem. It's just strange that with the procedure Init and the same code it works but using a constructor it doesn't. And init is in the record, just replace the constructor with the procedure: 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 PMyRec = ^TMyRec;TMyRec = record  Enclosing: PMyRec;  Name: String;  procedure Init(const aName: String);end; ---------------------------------------------------------------------- Thaddy · 2024-06-11 17:14 That is not where the error comes from. That part of the initial code is OK (See my proof). ---------------------------------------------------------------------- 440bx · 2024-06-11 17:15 Quote from: JdeHaan on June 11, 2024, 05:48:49 pmThis runs without any problems. How can this be explained? At first sight it looks like the only explanation is that there is a bug in how constructors are implemented in advanced records. this other thread points out a similar problem, https://forum.lazarus.freepascal.org/index.php/topic,50112.0.html the two might be related. ---------------------------------------------------------------------- Thaddy · 2024-06-11 17:18 Quote from: 440bx on June 11, 2024, 07:15:23 pmAt first sight it looks like the only explanation is that there is a bug in how constructors are implemented in advanced records. No. He needs to show more code on how he uses it. His initial code simply works. There is no bug in the implementation. Period.  The bug is his. Show more code. ---------------------------------------------------------------------- JdeHaan · 2024-06-11 17:40 It's a compiler project with multiple units, incl a byte code generator and a VM - 1000s of code lines. Believe me the only change I made was replacing the use of the record constructor by the Init procedure... BTW, I am planning to publish the code and describing document on GitHub soon ---------------------------------------------------------------------- Thaddy · 2024-06-11 18:28 You are looking at the wrong spot, because that part of your code was correct. As is proven. ---------------------------------------------------------------------- Warfley · 2024-06-11 19:21 Record constructors are weird: 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 type  TTest = record    Dummy: Integer;  class operator Initialize(var rec: TTest);  class operator Finalize(var rec: TTest);  class operator Copy(constref src: TTest; var dst: TTest);  class operator AddRef(var rec: TTest);   constructor Creat(AInteger: Integer);  end; class operator TTest.Initialize(var rec: TTest);begin  WriteLn('Initialize at ', IntPtr(@rec));end; class operator TTest.Finalize(var rec: TTest);begin  WriteLn('Finalize at ', IntPtr(@rec));end; class operator TTest.Copy(constref src: TTest; var dst: TTest);begin  WriteLn('Copy ', IntPtr(@src), ' to ', IntPtr(@dst));end; class operator TTest.AddRef(var rec: TTest);begin  WriteLn('AddRef at ', IntPtr(@rec));end; constructor TTest.Creat(AInteger: Integer);begin  WriteLn('Constructor on ', IntPtr(@Self));  Dummy := AInteger;end; procedure Test;var  rec: TTest;begin  WriteLn('Rec at ', IntPtr(@rec));  rec := TTest.Creat(42);end; Result: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Initialize at 20971052Initialize at 20971048Rec at 20971052Finalize at 20971048Constructor on 20971048Copy 20971048 to 20971052Finalize at 20971052Finalize at 20971048As you can see (except for the buggy first finalize), the constructor will be called on a temporary object, which will then be copied over to the actual variable. This means that when you take @Self in the constructor it will point to a temporary object which will be destroyed after it was copied over to the final destination ---------------------------------------------------------------------- JdeHaan · 2024-06-11 19:49 Thanks Warfley, that makes sense. So I can't use the constructor in this case. ---------------------------------------------------------------------- ASerge · 2024-06-12 00:32 Quote from: Warfley on June 11, 2024, 09:21:28 pmRecord constructors are weird: Not weird. The original code is weird. 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 MyRec := TMyRec.Create('ABC');Not 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 MyRec.Create('ABC');That is, the created new temporary variable is copied to MyRec. But inside the constructor, the address of the temporary variable is saved. ---------------------------------------------------------------------- JdeHaan · 2024-06-12 05:15 Thanks ASerge, 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 MyRec.Create(...); works indeed for my code. I made a wrong assumption that one has to use 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 MyRec := TMyRec.Create(...); similar as for classes. I wasn't aware of this. ---------------------------------------------------------------------- Thaddy · 2024-06-12 05:24 Quote from: JdeHaan on June 11, 2024, 09:49:35 pmThanks Warfley, that makes sense. So I can't use the constructor in this case. Yes, you can. The original code would not update current, it would stay the same, but with a little known trick the code will work as expected: records can contain const and const can be made writable. If you use it like this, current will always be the last record created instead of the first: 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 crashmenot; {$mode delphi}{$apptype console}{$J+}type PMyRec = ^TMyRec;TMyRec = record  const     Enclosing: PMyRec = nil; //!! writable const.  public    Name: String;    constructor Create(const aName: String);end; var  Current: PMyRec; // gets a value later on  constructor TMyRec.Create(const aName: String);begin  Name := aName;  Current := @Self;  Enclosing := Current;  writeln(self.name);end; var MyRec1,MyRec2,MyRec3:TMyrec; begin  MyRec1 := TMyRec.Create('ABC');  MyRec2 := TMyRec.Create('DEF');  MyRec3 := TMyRec.Create('GHI');  Writeln(MyRec1.Name);  Writeln(MyRec2.Name);  Writeln(MyRec3.Name);  writeln(current^.name);  Readln;end. For this to work across the program, you would need to implement  the copy operator to update current correctly. That is, unless you assign like this: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 current := @MyRec2;