The Delphi compiler in Berlin 10.1 introduces the concept of weak references.
With reference to the code below, without the [weak] attribute, when one and two is nil'd, the destructor is not called. With the [weak] attribute, the destructor is called. So, the weak references in the Delphi wiki now extends to Win32/Win64 (possibly iOS/OSX too?)
program
Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System
.
SysUtils;
type
IMyInterface =
interface
procedure
AddObjectRef(AMyInterface: IMyInterface);
end
;
TObjectOne =
class
(TInterfacedObject, IMyInterface)
private
FName:
string
[weak]anotherObj: IMyInterface;
public
constructor
Create(
const
AName:
);
destructor
Destroy; override;
main;
var
one, two: IMyInterface;
begin
one := TObjectOne
'one'
two := TObjectOne
'two'
one
AddObjectRef (two);
two
AddObjectRef (one);
two :=
nil
one :=
{ TObjectOne }
TObjectOne
anotherObj := AMyInterface;
inherited
Create;
FName := AName;
Destroy;
WriteLn
(FName,
' is being destroyed'