blob: b6aab1b3e8f9dc20335eb4edb75b176c0f80b6eb (
plain)
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
|
{ %opt=-gh }
program aIntfTest;
{$ifdef fpc}
{$mode delphi}
{$endif}
{$APPTYPE CONSOLE}
uses
SysUtils, Classes;
type
IMyIntf = interface
['{34326401-7B67-40FF-8E92-4587F65C8E24}']
function GetOwner: IMyIntf;
procedure Poing;
end;
type
TMYClass = clasS(TinterfacedObject, IMyIntf)
fRef: Integer;
public
function GetOwner: IMyIntf;
function QueryInterface(const IID: TGUID; out Obj): HRESULT; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
procedure Poing;
end;
{ TMYClass }
function TMYClass._AddRef: Integer;
begin
inc(fRef);
result := fRef;
Writeln('AddRef:'+inttostr(result));
end;
function TMYClass._Release: Integer;
begin
Dec(fRef);
result := FRef;
Writeln('Release:'+inttostr(result));
if result = 0 then Free;
end;
function TMYClass.GetOwner: IMyIntf;
begin
Writeln('GetOwner1');
result := nil;
Writeln('GetOwner2');
end;
function TMYClass.QueryInterface(const IID: TGUID; out Obj): HRESULT;
begin
if GetInterface(IID, Obj) then
result := S_OK else result := -1;
end;
var
r: IMyIntf;
procedure Test(x: IMyIntf);
begin
if x <> nil then x.Poing;
x := x.GetOwner;
if x <> nil then x.Poing;
end;
procedure TMYClass.Poing;
begin
writeln('poing');
end;
begin
HaltOnNotReleased := true;
r := TMYClass.Create;
Test(r);
Writeln('nil');
r := nil;
end.
|