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?)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
    procedure AddObjectRef(AMyInterface: IMyInterface);
    constructor Create(const AName: string);
    destructor Destroy; override;
  end;
 
procedure main;
var
  one, two: IMyInterface;
begin
  one := TObjectOne.Create('one');
  two := TObjectOne.Create('two');
  one.AddObjectRef (two);
  two.AddObjectRef (one);
  two := nil;
  one := nil;
end;
 
{ TObjectOne }
 
procedure TObjectOne.AddObjectRef(AMyInterface: IMyInterface);
begin
  anotherObj := AMyInterface;
end;
 
constructor TObjectOne.Create(const AName: string);
begin
  inherited Create;
  FName := AName;
end;
 
destructor TObjectOne.Destroy;
begin
  WriteLn(FName, ' is being destroyed');
  inherited;
end;
 
begin
  main;
end.