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
|
{%OPT=-gh}
program tdel1;
{$ifdef fpc}{$mode objfpc}{$h+}{$endif}
{ A test for correct refcounting when using different methods of casting
object to delegated COM interface. The requirement is no memleaks.
Delphi output: 3, 4, 3, 3, 3, 3
FPC output: 3, 4, 4, 4, 3, 3
}
const
STestInterface = '{3FB19775-F5FA-464C-B10C-D8137D742088}';
type
ITest = interface[STestInterface]
procedure DoSomething;
end;
TImpl=class(TInterfacedObject,ITest)
procedure DoSomething;
end;
TC1=class(TInterfacedObject,ITest)
private
FImpl: ITest;
public
constructor Create;
property impl: ITest read FImpl implements ITest;
end;
TC2=class(TInterfacedObject,ITest)
private
FImpl: ITest;
function GetImpl: ITest;
public
constructor Create;
property impl: ITest read GetImpl implements ITest;
end;
procedure TImpl.DoSomething;
begin
writeln('Doing something');
end;
function TC2.GetImpl: ITest;
begin
result:=FImpl;
end;
constructor TC1.Create;
begin
FImpl := TImpl.Create;
end;
constructor TC2.Create;
begin
FImpl := TImpl.Create;
end;
var
C1: TC1;
C2: TC2;
I: ITest;
ref: Integer;
begin
C1 := TC1.Create;
C2 := TC2.Create;
writeln('Testing typecasting...');
I := ITest(C1);
ref := I._Addref;
I._Release;
writeln('When delegating by field, refcount=', ref);
I := ITest(C2);
ref := I._Addref;
I._Release;
writeln('When delegating by function, refcount=', ref);
{clean up}
I := nil;
C1.Free;
C2.Free;
writeln('Testing ''as'' operator...');
C1 := TC1.Create;
C2 := TC2.Create;
I := C1 as ITest;
ref := I._Addref;
I._Release;
writeln('When delegating by field, refcount=', ref);
I := C2 as ITest;
ref := I._Addref;
I._Release;
writeln('When delegating by function, refcount=', ref);
{clean up}
I := nil;
C1.Free;
C2.Free;
writeln('Testing GetInteface()...');
C1 := TC1.Create;
C2 := TC2.Create;
C1.GetInterface(ITest, I);
ref := I._Addref;
I._Release;
writeln('When delegating by field, refcount=', ref);
C2.GetInterface(ITest, I);
ref := I._Addref;
I._Release;
writeln('When delegating by function, refcount=', ref);
{clean up}
I := nil;
C1.Free;
C2.Free;
end.
|