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?
- There are many
Free
's like that in my program. Of course, transferring theFree
code to something else is possible, but I would like it more elegant. - Program needs to compile in Windows back to XE2, on Linux on 10.2. I read 10.3 leaves out ARC, which may solve the issue, but 10.3 is costly.
- Program changes needed and
{$IFDEF ...}
directives preferably minimized.
Please tell me your suggestions.