blob: 088649bbad67c4d8519333ae34cf9241a789f69b (
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
|
// Tests storing interfaces in VariantArray
{$ifdef fpc}{$mode objfpc}{$h+}{$endif}
{$apptype console}
uses sysutils, variants;
type
ITag = interface(IInterface)['{26EBC417-D394-4561-906A-202F32A919EA}']
function GetTag: Integer;
end;
tmyobj=class(TInterfacedObject,ITag)
private
FTag: Integer;
function GetTag: Integer;
public
constructor Create(aTag: Integer);
destructor Destroy; override;
end;
var
FreeCount: Integer;
constructor tmyobj.create(aTag: Integer);
begin
inherited Create;
FTag:=aTag;
end;
destructor tmyobj.destroy;
begin
writeln('Destroy: ', FTag);
Inc(FreeCount);
inherited;
end;
function tmyobj.gettag: integer;
begin
result:=FTag;
end;
var
values: Variant;
i: Integer;
begin
Values := VarArrayCreate([0, 4], varUnknown);
for i := 0 to 4 do
Values[i] := tmyobj.Create(i) as IInterface;
for i := 0 to 4 do
begin
if (IInterface(Values[i]) as ITag).GetTag <> i then
Halt(i);
end;
FreeCount := 0;
Values := 0;
writeln(FreeCount);
// check for correct number of destroyed objects won't work because one of them
// is released after this point.
end.
|