Changeset 7 for os/trunk/Xvcl


Ignore:
Timestamp:
Jun 2, 2013, 6:59:07 PM (12 years ago)
Author:
chronos
Message:
  • Fixed: Components now have list of childs and free them on destruction.
  • Fixed: Memory leaks.
Location:
os/trunk/Xvcl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • os/trunk/Xvcl/Xvcl.Classes.pas

    r5 r7  
    22
    33interface
     4
     5uses
     6  Xvcl.Generics;
    47
    58type
     
    5154    FOwner: TComponent;
    5255    FName: string;
     56    FComponents: TObjectList<TComponent>;
     57    procedure SetOwner(const Value: TComponent);
    5358  public
    5459    constructor Create; virtual;
    55     property Owner: TComponent read FOwner write FOwner;
     60    destructor Destroy; override;
     61    property Components: TObjectList<TComponent> read FComponents;
     62    property Owner: TComponent read FOwner write SetOwner;
    5663    property Name: string read FName write FName;
    5764  end;
     
    161168constructor TComponent.Create;
    162169begin
     170  FComponents := TObjectList<TComponent>.Create;
     171end;
     172
     173destructor TComponent.Destroy;
     174begin
     175  FComponents.Destroy;
     176  inherited;
     177end;
     178
     179procedure TComponent.SetOwner(const Value: TComponent);
     180begin
     181  if Value = FOwner then Exit;
     182  if not Assigned(Value) then FOwner.Components.Remove(Self);
     183  FOwner := Value;
     184  if Assigned(Value) then FOwner.Components.Add(Self);
    163185end;
    164186
     
    181203  Self.Y := Y;
    182204end;
    183 
    184205
    185206function TPoint.IsZero: Boolean;
  • os/trunk/Xvcl/Xvcl.Generics.pas

    r5 r7  
    3333    function Compare(Item1, Item2: T): Integer;
    3434    procedure Exchange(Index1, Index2: Integer);
     35    procedure Clear;
    3536    property Count: Integer read FCount write SetCount;
    3637    property Items[Index: Integer]: T read GetItem write SetItem; default;
     38  end;
     39
     40  TObjectList<T: class> = class(TList<T>)
     41    OwnsObjects: Boolean;
     42    constructor Create; virtual;
     43    destructor Destroy; override;
    3744  end;
    3845
     
    6168  FItems[Count - 1] := Item;
    6269  Result := Count - 1;
     70end;
     71
     72procedure TList<T>.Clear;
     73begin
     74  Count := 0;
    6375end;
    6476
     
    149161end;
    150162
     163{ TObjectList<T> }
     164
     165constructor TObjectList<T>.Create;
     166begin
     167  OwnsObjects := True;
     168end;
     169
     170destructor TObjectList<T>.Destroy;
     171var
     172  I: Integer;
     173begin
     174  if OwnsObjects then
     175  for I := 0 to Count - 1 do
     176    TObject(FItems[I]).Destroy;
     177  Clear;
     178  inherited;
     179end;
     180
    151181end.
Note: See TracChangeset for help on using the changeset viewer.