record default lazarus / 21-General ---------------------------------------------------------------------- ginoo · 2025-09-01 05:03 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  ciccio = record    a : Integer;    class operator Initialize(var value:ciccio);  end; var  c : ciccio; { ciccio } class operator ciccio.Initialize(var value: ciccio);begin  value.a:=3;end; begin  WriteLn(c.a);  c.a := 10;  WriteLn(c.a);  c := Default(ciccio);  WriteLn(c.a); end.      In my opinion, calling the record's default should call Initialize and set ciccio.a = 3. What is the way, if any, to reinitialize a record after modifying it? ---------------------------------------------------------------------- Thaddy · 2025-09-01 05:07 The behavior of Default() is as documented. To reset the record to 3, set the field to 3 ---------------------------------------------------------------------- H₂SO₄ · 2025-09-01 06:24 Quote from: ginoo on September 01, 2025, 07:03:07 amWhat is the way, if any, to reinitialize a record after modifying it? 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 Initialize(c); ---------------------------------------------------------------------- ginoo · 2025-09-01 07:30 Quote from: Khrys on September 01, 2025, 08:24:43 amQuote from: ginoo on September 01, 2025, 07:03:07 amWhat is the way, if any, to reinitialize a record after modifying it? 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 Initialize(c); It works. Does it make sense to also call "finalize" at the end after calling "Initialize," or is it not important since it's a non-dynamic variable? ---------------------------------------------------------------------- Thaddy · 2025-09-01 08:03 Only if your record contains managed types. ---------------------------------------------------------------------- ginoo · 2025-09-01 08:11 Quote from: Thaddy on September 01, 2025, 10:03:01 amOnly if your record contains managed types. Dear @Thaddy, I'm considering using inizialeze because it's cleaner to have a single initializer for record fetching with a lot of code. There are probably other ways, but I think this one is valid. ---------------------------------------------------------------------- Thaddy · 2025-09-01 08:21 I was referring to finalize... ---------------------------------------------------------------------- Warfley · 2025-09-01 11:21 Quote from: ginoo on September 01, 2025, 07:03:07 amIn my opinion, calling the record's default should call Initialize and set ciccio.a = 3. What is the way, if any, to reinitialize a record after modifying it? Yes your opinion is right and in trunk this is fixed. Until it is in stable instead of using default do the following: 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 generic procedure SetDefault(var AVar: T);begin  Finalize(AVar);  Initialize(AVar);end;Or 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 generic function ManagedDefault:T;begin {nothing needed} end; ---------------------------------------------------------------------- ginoo · 2025-09-01 12:04 Just curious, I tried putting a managed type on the record and not using finalize. However, it says there's no memory leak. What's the problem with not calling "finalize"? 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 typeciccio = record   a : String;   class operator Initialize(var value:ciccio); end; var c : ciccio; { ciccio } class operator ciccio.Initialize(var value: ciccio);begin value.a:='pippo';end; begin WriteLn(c.a); c.a := 'caio'; WriteLn(c.a); Initialize(c); c.a := 'caio2'; WriteLn(c.a); Initialize(c); c.a := 'caio3'; WriteLn(c.a); Initialize(c); WriteLn(c.a);end.     0 memory blocks allocated : 0/0 0 memory blocks freed     : 0/0 0 unfreed memory blocks : 0 True heap size : 0 True free heap : 0 ---------------------------------------------------------------------- Warfley · 2025-09-01 12:58 Because StringConstants are not reference counted, because they are hardcoded in the program data. Change your code to the following: 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 c.a:='calo';UniqueString(c.a);...And try again. UniqueString makes sure that you have a unique copy of the string, which allocates a new memory object ---------------------------------------------------------------------- ginoo · 2025-09-01 13:12 Yes, you're right. Thank you all. ---------------------------------------------------------------------- PascalDragon · 2025-09-02 19:15 Quote from: ginoo on September 01, 2025, 07:03:07 am  ciccio = record     a : Integer;     class operator Initialize(var value:ciccio);   end; var   c : ciccio; [snip] Please use [code][/code]-tags to make the code better visible and also to avoid the forum software potentially interpreting the code. ---------------------------------------------------------------------- ginoo · 2025-09-03 09:56 Done ---------------------------------------------------------------------- PascalDragon · 2025-09-05 20:42 Quote from: ginoo on September 03, 2025, 11:56:35 amDone Thank you.