In delphi sydney, does setting [weak] in front of an object (not an interface) make a penalty ? Exemple :
TMyObject = class(Tobject)
Private
[weak] FOwner: TMyObject;
....
end;
I ask because I know that internally [weak] references are stored in a list and thus it's have some drawbacks (regarding speed). As now with Sydney ARC is gone there is no need anymore to put [weak] in front of an object (as far as I know), but as I want to keep my code compatible with Rio and less I ask if I can safely leave the [weak] reference without suffering of useless performance lost (in sydney)
there is a teeny-weeny problem with the [weak] attribute. It denotes a zeroing weak reference that will be zeroed (niled) when the object it points to is no longer valid. In order to do that, the compiler has to track such objects at runtime and that introduces some overhead. If you are tracking many such references, that can introduce a significant performance loss.
[weak]
attribute for object references was only implemented on ARC compilers. On classic, non-ARC compilers [weak]
attribute did nothing and had no penalty when used on object references.
Since 10.4 Sydney no longer has ARC compilers, [weak]
attribute is no longer needed, but it can be used for maintaining backward compatibility. It will have no impact on code that is compiled with non-ARC compilers.
That [weak]
has no impact on non-ARC compilers can be easily inspected through CPU view.
var
Obj: TObject;
[weak] WObj: TObject;
begin
Obj := TObject.Create;
WObj := Obj;
Obj.Free;
end;
On Android ARC compiler 10.3 assigning Obj
to weak WObj
will call _InstWeakCopy
procedure to keep track of weak reference:
Same code when compiled with 10.4 Android compiler no longer has call to _InstWeakCopy
Note: This answer strictly covers behavior of [weak]
attribute when used on object references. Used on interface references [weak]
works the same as before, since it was introduced in non-ARC compilers in 10.1 Berlin.