Just realized:
Just realized:
For me the most dangerous thing in ARC is the fact that you can never rely that a destructor gets called when you call Free. It is twice as dangerous because Embarcadero are advertising "debug mobile apps on Windows", which is impossible because ARC is missing on the Windows platform and therefore very different behaviour can occur.
Consider this code:
unit uFreeTest;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
FMX.Memo;
type
TForm1 = class(TForm)
public
FSomeObject: TObject;
procedure InitializeNewForm; override;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
type
TMyObject = class
public
[weak] Console: TMemo;
destructor Destroy; override;
end;
destructor TMyObject.Destroy;
begin
Console.Lines.Add('TMyObject.Destroy');
inherited;
end;
{ TForm1 }
procedure TForm1.InitializeNewForm;
var
Memo: TMemo;
xObj: TMyObject;
begin
inherited;
Memo := TMemo.Create(Self);
Memo.Parent := Self;
Memo.Align := TAlignLayout.alClient;
xObj := TMyObject.Create;
try
xObj.Console := Memo;
FSomeObject := xObj;
finally
xObj.Free;
end;
end;
end.
When running on Win32, the line 'TMyObject.Destroy' appears in the memo because the destructor is called normally.
In ARC (NextGen), the memo is clear. In more complex scenarios you basically never know if there is any reference left in your code. (Just image sending an object reference to a custom procedure which stores the reference somewhere.)
Hence you can basically never rely on the fact that the destructor gets called along with Free.
-> Yes, handle classes just like interfaces.
-> Therefore I am convinced that there shouldn't be any Free method in NextGen at all and the public destructor should be moved to the protected section (interfaces haven't got a free method either). Yes and Embarcadero should write NextGen for Win/Mac as well so that mobile projects behave correctly on all platforms.
-> And please, separate NextGen from Desktop development, they are just too different...
Please discuss :)
For me the most dangerous thing in ARC is the fact that you can never rely that a destructor gets called when you call Free. It is twice as dangerous because Embarcadero are advertising "debug mobile apps on Windows", which is impossible because ARC is missing on the Windows platform and therefore very different behaviour can occur.
Consider this code:
unit uFreeTest;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
FMX.Memo;
type
TForm1 = class(TForm)
public
FSomeObject: TObject;
procedure InitializeNewForm; override;
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
type
TMyObject = class
public
[weak] Console: TMemo;
destructor Destroy; override;
end;
destructor TMyObject.Destroy;
begin
Console.Lines.Add('TMyObject.Destroy');
inherited;
end;
{ TForm1 }
procedure TForm1.InitializeNewForm;
var
Memo: TMemo;
xObj: TMyObject;
begin
inherited;
Memo := TMemo.Create(Self);
Memo.Parent := Self;
Memo.Align := TAlignLayout.alClient;
xObj := TMyObject.Create;
try
xObj.Console := Memo;
FSomeObject := xObj;
finally
xObj.Free;
end;
end;
end.
When running on Win32, the line 'TMyObject.Destroy' appears in the memo because the destructor is called normally.
In ARC (NextGen), the memo is clear. In more complex scenarios you basically never know if there is any reference left in your code. (Just image sending an object reference to a custom procedure which stores the reference somewhere.)
Hence you can basically never rely on the fact that the destructor gets called along with Free.
-> Yes, handle classes just like interfaces.
Please discuss :)
Comments
Post a Comment