Ohhhh, die neuen Custom Managed Records verhalten sich übrigens ganz anders.
Sie werden "sofort" freigegeben, wenn sie nicht mehr benötigt werden ... nicht erst zum Ende des aktuellen Scopes.
Delphi-Quellcode:
type
TTest = record
Name: string;
constructor Create(S: string);
class operator Implicit(S: string): TTest;
class operator Initialize(out Dest: TTest);
class operator Finalize(var Dest: TTest);
class operator Assign(var Dest: TTest; const [ref] Src: TTest);
end;
constructor TTest.Create(S: string);
begin
Name := S;
ShowMessage('Create ' + Name);
end;
class operator TTest.Implicit(S: string): TTest;
begin
Result.Name := S;
ShowMessage('Impicit ' + Result.Name);
end;
class operator TTest.Finalize(var Dest: TTest);
begin
ShowMessage('Final ' + Dest.Name);
Dest.Name := '';
end;
class operator TTest.Initialize(out Dest: TTest);
begin
Dest.Name := 'Init';
ShowMessage('Init');
end;
class operator TTest.Assign(var Dest: TTest; const [Ref] Src: TTest);
begin
if Dest.Name = 'Init' then
ShowMessage('Assign ' + Src.Name)
else
ShowMessage('Assign ' + Src.Name + '>' + Dest.Name);
Dest.Name := Src.Name;
end;
procedure TForm11.FormCreate(Sender: TObject);
var
Var1a, Var1, Var2a, Var2, Var3a, Var3, Var4a, Var4: TTest;
begin // Init Var1a..Var4
Var1a := 'Var1a'; // Init/Implicit (Temp)Var1a / Assign Var1a / Final (Temp)Var1a
Var1 := TTest.Create('Var1'); // Init/Create (New)Var1 / Assign Var1 / Final (New)Var1
var Inline1 := TTest.Create('Inline1'); // Init/Create Inline1
TTest.Create('Temp1'); // Init/Create/Final Temp1
Beep;
if Tag = 0 then
Var2a := 'Var2a'; // Init/Implicit (Temp)Var2a / Assign Var2a / Final (Temp)Var2a
if Tag = 0 then
Var2 := TTest.Create('Var2'); // Init/Create (New)Var2 / Assign Var2 / Final (New)Var2
if Tag = 0 then
var Inline2 := TTest.Create('Inline2'); // Init/Create/Final Inline2
if Tag = 0 then
TTest.Create('Temp2'); // Init/Create/Final Temp2
Beep;
if Tag = 0 then begin
Var3a := 'Var3a'; // Init/Implicit (Temp)Var3a / Assign Var3a / Final (Temp)Var3a
Var3 := TTest.Create('Var3'); // Init/Create (New)Var3 / Assign Var3 / Final (New)Var3
var Inline3 := TTest.Create('Inline3'); // Init/Create Inline3
TTest.Create('Temp3'); // Init/Create/Final Temp3
end; // ###NOT FInal Temp3 / Final Inline3
Beep;
if Tag = 0 then begin
Var4a := 'Var4a'; // Init/Implicit (Temp)Var4a / Assign Var4a / Final (Temp)Var4a
Var4 := TTest.Create('Var4'); // Init/Create (New)Var4 / Assign Var4 / Final (New)Var4
var Inline4 := TTest.Create('Inline4'); // Init/Create Inline4
TTest.Create('Temp4'); // Init/Create/Final Temp4
Beep;
begin
TTest.Create('Temp6'); // Init/Create/Final Temp6
Beep;
TTest.Create('Temp7'); // Init/Create/Final Temp7
Beep;
end; // ###NOT Final Temp7/Temp6
Beep;
begin
var Temp8 := TTest.Create('Temp8'); // Init/Create Temp8
Beep;
Temp8 := TTest.Create('Temp9'); // Init/Create (New)Temp9 / Assign Temp9>Temp8 / Final (New)Temp9
Beep;
end; // NOT Final (Temp8)Temp9
Beep;
ShowMessage('Show'); // Show
Beep;
end; // ###NOT Final Temp4 / Final Inline4
Beep;
end; // ###NOT Final Temp1 / Final Inline1/Var4..Var1a