Thema: Weak-TList ?

Einzelnen Beitrag anzeigen

Benutzerbild von Stevie
Stevie

Registriert seit: 12. Aug 2003
Ort: Soest
4.027 Beiträge
 
Delphi 10.1 Berlin Enterprise
 
#3

AW: Weak-TList ?

  Alt 26. Feb 2015, 08:39
Delphi-Quellcode:
program WeakRefListTests;

uses
  TestFramework,
  TestInsight.DUnit,
  Spring,
  Spring.Collections;

type
  TWeakRefListTest = class(TTestCase)
  published
    procedure ListDoesNotKeepStrongReference;
  end;

  IRefCounted = interface
    function GetRefCount: Integer;
    property RefCount: Integer read GetRefCount;
  end;

  TRefCounted = class(TInterfacedObject, IRefCounted);

procedure TWeakRefListTest.ListDoesNotKeepStrongReference;
var
  sut: IList<WeakReference<IRefCounted>>;
  ref: IRefCounted;
begin
  sut := TCollections.CreateList<WeakReference<IRefCounted>>;
  ref := TRefCounted.Create;
  sut.Add(ref);
  CheckEquals(1, ref.RefCount);
  ref := nil;
  CheckFalse(sut[0].IsAlive);
  ref := sut[0];
  CheckNull(ref);
end;

begin
  RegisterTest(TWeakRefListTest.Suite);
  RunRegisteredTests();
end.
Stefan
“Simplicity, carried to the extreme, becomes elegance.” Jon Franklin

Delphi Sorcery - DSharp - Spring4D - TestInsight
  Mit Zitat antworten Zitat