I have this simple program, written in Delphi 10.2, and running nicely on Windows, but crashing on Linux.
The essence is that the class used has code to be executed in its destructor.
type
Kwak = class
public
index: integer;
constructor Create(index:integer);
destructor Free;
end;
constructor Kwak.Create(index:integer);
begin
self.index := index;
writeln('Welcome Kwak '+inttostr(index));
end;
destructor Kwak.Free;
begin
writeln('Bye Kwak '+inttostr(index));
end;
If I use this in a calling procedure, like this one:
procedure myProc1;
var
myKwak:Kwak;
begin
myKwak := Kwak.Create(15);
myKwak.Free;
end;
This runs fine on Windows, but causes a segmentation error on Linux the moment myKwak
leaves scope (the end
is encountered in myProc1
).
I guess this all has to do with Automatic Reference Counting on Linux compiler.
If I use FreeAndNil()
, the program doesn't crash, but doesn't call the destructor either.
What is an elegant solution?
Free
's like that in my program. Of course, transferring the Free
code to something else is possible, but I would like it more elegant.{$IFDEF ...}
directives preferably minimized.Please tell me your suggestions.
destructor Free; // <-- WRONG!
This is wrong. The correct destructor is called Destroy()
instead, and it is virtual
in TObject
so you need to override
it in derived classes:
type
Kwak = class
public
index: integer;
constructor Create(index:integer);
destructor Destroy; override;
end;
constructor Kwak.Create(index:integer);
begin
inherited Create;
self.index := index;
writeln('Welcome Kwak '+inttostr(index));
end;
destructor Kwak.Destroy;
begin
writeln('Bye Kwak '+inttostr(index));
inherited;
end;
On non-ARC systems, TObject.Free()
is a non-virtual instance method that calls the Destroy()
destructor if Self
is not nil
.
On ARC systems, the compiler silently replaces all calls to Free()
with a nil
assignment, decrementing an object's reference count. This is so the same code can be used with similar semantics on both ARC and non-ARC systems. An object's Destroy()
destructor is called when its reference count falls to 0.