-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProject1.dpr
131 lines (113 loc) · 2.98 KB
/
Project1.dpr
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.Classes,
System.SysUtils,
SafeComponentReference in 'SafeComponentReference.pas';
type
TSafeTestReference = TSafeComponentReference<TComponent>;
var
LC: Integer;
Components: TArray<TComponent>;
Reference1, Reference2: TSafeTestReference;
procedure CheckAssigned(const AValue: TComponent);
begin
Assert(Assigned(AValue));
end;
procedure CheckUnassigned(const AValue: TComponent);
begin
Assert(not Assigned(AValue));
end;
procedure LocalTest1;
var
LocalComponent: TComponent;
LocalReference1, LocalReference2: TSafeTestReference;
begin
LocalComponent := TComponent.Create(nil);
try
LocalReference1 := nil;
LocalReference2 := nil;
CheckUnassigned(LocalReference1);
CheckUnassigned(LocalReference2);
LocalReference1.Target := LocalComponent;
LocalReference2 := LocalReference1;
CheckAssigned(LocalReference1);
CheckAssigned(LocalReference2);
FreeAndNil(LocalComponent);
CheckUnassigned(LocalReference1);
CheckUnassigned(LocalReference2);
finally
if Assigned(LocalComponent) then
LocalComponent.Free;
end;
end;
procedure LocalTest2;
var
LocalComponent: TComponent;
LocalReference1, LocalReference2: TSafeTestReference;
begin
LocalComponent := TComponent.Create(nil);
try
LocalReference1 := LocalComponent;
LocalReference2 := LocalReference1;
CheckAssigned(LocalReference1);
CheckAssigned(LocalReference2);
FreeAndNil(LocalComponent);
CheckUnassigned(LocalReference1);
CheckUnassigned(LocalReference2);
finally
if Assigned(LocalComponent) then
LocalComponent.Free;
end;
end;
begin
ReportMemoryLeaksOnShutdown := True;
for LC := 0 to 2 do
Components := Components + [TComponent.Create(nil)];
try
LC := 0;
try
Reference1 := Components[LC];
Reference2 := Reference1;
CheckAssigned(Reference2);
CheckAssigned(Reference1);
FreeAndNil(Components[LC]);
CheckUnassigned(Reference1);
CheckUnassigned(Reference2);
Inc(LC);
Reference1 := nil;
Reference2 := nil;
CheckUnassigned(Reference1);
CheckUnassigned(Reference2);
Reference1.Target := Components[LC];
Reference2 := Reference1;
CheckAssigned(Reference1);
CheckAssigned(Reference2);
FreeAndNil(Components[LC]);
CheckUnassigned(Reference1);
CheckUnassigned(Reference2);
Inc(LC);
Reference1 := TSafeTestReference(Components[LC]);
Reference2 := Reference1;
CheckAssigned(Reference1);
CheckAssigned(Reference2);
FreeAndNil(Components[LC]);
CheckUnassigned(Reference1);
CheckUnassigned(Reference2);
LocalTest1;
LocalTest2;
WriteLn('Voila!');
finally
for LC := Low(Components) to High(Components) do
if Assigned(Components[LC]) then
Components[LC].Free;
end;
except
on E: Exception do
begin
Writeln(E.ClassName, ': ', E.Message);
end;
end;
ReadLn;
end.