management operator Finalize is unexpectedly called once more if record constructor comes into play
Original Reporter info from Mantis: petkos
-
Reporter name:
Original Reporter info from Mantis: petkos
- Reporter name:
Description:
When record is created via record conctructor, the finilizer is triggert immediately after the construcor (unexpected) and once again after the record variable goes out of scope (expected).
To get the expected result switch from tmp.Create(5) to tmp.Init(5).
In any case i would expect that the finilize method is called as many times as the initialize method.
Thank you
Steps to reproduce:
program project1;
{$MODE OBJFPC}{$MODESWITCH AdvancedRecords}
uses
Classes, SysUtils;
type
TTest = class
private
_InstanceRef: QWORD;
public
procedure Echo;
constructor Create(InstanceRef: QWORD);
end;
TStructWithObject = record
public
var
innerobj : TTest;
size : Integer;
class operator initialize(var Me: TStructWithObject);
class operator finalize (var Me: TStructWithObject);
constructor Create(NewSize: Integer);
procedure Init(NewSize: Integer);
end;
constructor TTest.Create(InstanceRef: QWORD);
begin
inherited Create;
_InstanceRef:= InstanceRef;
end;
procedure TTest.Echo;
begin
writeln(inttostr(_InstanceRef));
end;
constructor TStructWithObject.Create(NewSize: Integer);
begin
Init(NewSize);
end;
procedure TStructWithObject.Init(NewSize: Integer);
begin
size:= NewSize;
end;
class operator TStructWithObject.initialize(var Me: TStructWithObject);
begin
writeln('initialize object: ' + inttostr(QWORD(@Me)));
Me.innerobj:= TTest.Create(QWORD(@Me));
end;
class operator TStructWithObject.finalize(var Me: TStructWithObject);
begin
writeln('finalize object: ' + inttostr(QWord(@Me)));
FreeAndNil(Me.innerobj);
end;
procedure DoIt;
var
tmp: TStructWithObject;
begin
tmp.Create(5);
//tmp.Init(5);
if Assigned(tmp.innerobj) then tmp.innerobj.Echo();
end;
begin
DoIt;
readln();
end.
Additional information:
Mantis conversion info:
- Mantis ID: 33157
- OS: Wincows
- OS Build: 10
- Build: 38156
- Platform: x64
- Version: 3.1.1