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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
{%OPT=-gh}
program tdel2;
{$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.
}
uses
//heaptrc,
SysUtils;
const
STestInterface = '{3FB19775-F5FA-464C-B10C-D8137D742088}';
type
ITest = interface[STestInterface]
function GetRefCount: Integer;
end;
TImpl = class(TInterfacedObject,ITest)
function GetRefCount: Integer;
end;
TTest = class(TInterfacedObject)
public
constructor Create; virtual; abstract;
end;
TTestClass = class of TTest;
TC1 = class(TTest,ITest)
private
FImpl: ITest;
public
constructor Create; override;
property impl: ITest read FImpl implements ITest;
end;
TC2 = class(TTest,ITest)
private
FImpl: ITest;
function GetImpl: ITest;
public
constructor Create; override;
property impl: ITest read GetImpl implements ITest;
end;
TC3 = class(TTest,ITest)
private
FImpl: TImpl;
public
constructor Create; override;
destructor Destroy; override;
property impl: TImpl read FImpl implements ITest;
end;
function TImpl.GetRefCount: Integer;
begin
Result := refcount;
end;
constructor TC1.Create;
begin
FImpl := TImpl.Create;
end;
constructor TC2.Create;
begin
FImpl := TImpl.Create;
end;
function TC2.GetImpl: ITest;
begin
result:=FImpl;
end;
constructor TC3.Create;
begin
FImpl := TImpl.Create;
FImpl._AddRef;
end;
destructor TC3.Destroy;
begin
FImpl._Release;
inherited Destroy;
end;
type
TTestCase = record
c: TTestClass;
by: String;
end;
var
tests: array[0..2] of TTestCase = (
(c:TC1; by:'intf field'),
(c:TC2; by:'intf function'),
(c:TC3; by:'class field')
);
failed: Boolean = false;
procedure fail(const by: String);
begin
writeln(' When delegating by ', by, ', failed');
failed := true;
end;
procedure succ(const by: String);
begin
writeln(' When delegating by ', by);
end;
procedure succ(const by: String; R: Integer);
begin
writeln(' When delegating by ', by, ', refcount=', R);
end;
procedure succ(const by: String; const S: String);
begin
writeln(' When delegating by ', by, ', Classname=', S);
end;
var
T: Integer;
C: TInterfacedObject;
I: ITest;
P: Pointer;
O: TImpl;
begin
(*******************************************************************************
* GetInterface function
*******************************************************************************)
writeln('Testing GetInteface()...');
for T := 0 to High(tests) do
begin
C := tests[T].c.Create;
if C.GetInterface(ITest, I) then
succ(tests[T].by, I.GetRefCount)
else
fail(tests[T].by);
I := nil;
C.Free;
end;
(*******************************************************************************
* GetInterfaceByStr function
*******************************************************************************)
writeln('Testing GetInterfaceByStr()...');
for T := 0 to High(tests) do
begin
C := tests[T].c.Create;
if C.GetInterfaceByStr(STestInterface, I) then
succ(tests[T].by, I.GetRefCount)
else
fail(tests[T].by);
I := nil;
C.Free;
end;
(*******************************************************************************
* GetInterfaceWeak function
*******************************************************************************)
writeln('Testing GetInterfaceWeak()...');
for T := 0 to High(tests) do
begin
C := tests[T].c.Create;
P := nil;
if C.GetInterfaceWeak(ITest, P) then
succ(tests[T].by, ITest(P).GetRefCount)
else
fail(tests[T].by);
P := nil;
C.Free;
end;
(*******************************************************************************
* Supports function
*******************************************************************************)
writeln('Testing ''supports'' function...');
for T := 0 to High(tests) do
begin
C := tests[T].c.Create;
if Supports(C, ITest, I) then
succ(tests[T].by, I.GetRefCount)
else
fail(tests[T].by);
I := nil;
C.Free;
end;
(*******************************************************************************
* IS operator
*******************************************************************************)
writeln('Testing ''object is interface'' operator...');
for T := 0 to High(tests) do
begin
C := tests[T].c.Create;
if C is ITest then
succ(tests[T].by)
else
fail(tests[T].by);
C.Free;
end;
writeln('Testing ''interface is interface'' operator...');
for T := 0 to High(tests) do
begin
C := tests[T].c.Create;
P := nil;
if C.GetInterfaceWeak(IUnknown, P) then
begin
if IUnknown(P) is ITest then
succ(tests[T].by)
else
fail(tests[T].by);
end else
fail(tests[T].by);
P := nil;
C.Free;
end;
writeln('Testing ''interface is object'' operator...');
for T := 0 to High(tests) do
begin
C := tests[T].c.Create;
I := C as ITest;
if I<>nil then
begin
if I is TImpl then
succ(tests[T].by)
else
fail(tests[T].by);
end else
fail(tests[T].by);
I := nil;
C.Free;
end;
(*******************************************************************************
* AS operator
*******************************************************************************)
writeln('Testing ''object as interface'' operator...');
for T := 0 to High(tests) do
begin
C := tests[T].c.Create;
I := C as ITest;
if I<>nil then
succ(tests[T].by, I.GetRefCount)
else
fail(tests[T].by);
I := nil;
C.Free;
end;
writeln('Testing ''interface as interface'' operator...');
for T := 0 to High(tests) do
begin
C := tests[T].c.Create;
P := nil;
if C.GetInterfaceWeak(IUnknown, P) then
begin
I := IUnknown(P) as ITest;
if I<>nil then
succ(tests[T].by, I.GetRefCount)
else
fail(tests[T].by);
I := nil;
end else
fail(tests[T].by);
P := nil;
C.Free;
end;
writeln('Testing ''interface as object'' operator...');
for T := 0 to High(tests) do
begin
C := tests[T].c.Create;
I := C as ITest;
if I<>nil then
begin
O := I as TImpl;
if O<>nil then
succ(tests[T].by, O.Classname)
else
fail(tests[T].by);
end else
fail(tests[T].by);
I := nil;
C.Free;
end;
if failed then
Halt(1);
end.
|