Board index » delphi » don't know if list works fine

don't know if list works fine

hi

I have a problem with a linked list.
I want to store objects in my list and when I free the list, all objects
should also be freed.
When I add a TEdit, for example, and then clear the list, the Edit should be
destroyed.

var
  NewEdit: TEdit;
begin
  NewEdit := TEdit.Create;
  List.Add(NewEdit);
  ....
  List.Clear;
end;

I'm not really sure, if all the memory is freed.
I put my list and the important procedures below.

Perhaps someone can tell me, if something's wrong with it.

Thx

////////////////// unit LList /////////////////////

unit LList;

interface

Type

  PListEntry = ^TListEntry;
  TListEntry = record
    Data : TObject;
    Prev, Next : PListEntry;
  end;

  TLinkedList = class( TObject )
  private
    FFirst : PListEntry;
    FLast  : PListEntry;
    FCount : Integer;

  public

    constructor Create;
    destructor Destroy; override;

    procedure Add( Data : TObject );

    procedure Clear;

    property Count : Integer read FCount;
  end;

implementation

constructor TLinkedList.Create;
begin
  Inherited;
  FFirst := nil;
  FLast := nil;
  FCount := 0;
end;

destructor TLinkedList.Destroy;
begin
  Clear;
  Inherited;
end;

procedure TLinkedList.Add( Data : TObject );
var
  NewItem : PListEntry;
begin
  New( NewItem );
  NewItem^.Data := Data;
  NewItem^.Next := nil;
  NewItem^.Prev := FLast;
  If Assigned(FLast) then FLast^.Next := NewItem
  else FFirst := NewItem;
  FLast := NewItem;
  Inc( FCount );
end;

procedure TLinkedList.Clear;
var
  Entry : PListEntry;
  Last : PListEntry;
begin
  Entry := FFirst;
  While Assigned(Entry) do begin
    Last := Entry;
    Entry := Entry^.Next;
    Last^.Data.Free;
    Dispose(Last);
  end;
  FFirst := nil;
  FLast := nil;
  FCount := 0;
end;

end.

 

Re:don't know if list works fine


Jan Theegarten heeft geschreven in bericht
<36ea6a31$0$65...@news.dpn.de>...
:hi
:
:I have a problem with a linked list.
:I want to store objects in my list and when I free the list, all objects
:should also be freed.
:When I add a TEdit, for example, and then clear the list, the Edit should
be
:destroyed.
:
:var
:  NewEdit: TEdit;
:begin
:  NewEdit := TEdit.Create;
:  List.Add(NewEdit);
:  ....
:  List.Clear;
:end;
:
:I'm not really sure, if all the memory is freed.
:I put my list and the important procedures below.
:
:Perhaps someone can tell me, if something's wrong with it.
:
:Thx

Jan,

Unless this is some mandatory programming exercise : you're re-inventing
the wheel. Delphi has a ready-built TList class. Have a look at it in
online-help, it has all the bells & whistles you need.

Your question however,  is fully justified. Even with Delphi's TList, you
need to destroy the object instances the list points to. With Delphi's
TList, this must be done by typecasting the pointers.

(assuming Delphi's TList )

TheList := TList.Create;
...
var
 i : integer;
...
  for i := 0 to TheList.Count -1 do
    Tedit(TheList[i]).Free;
...
  TheList.Free;

Regards,
Dirk Claessens
______________________________________________
dirk.claess...@village.ZZuunet.be
dirk.claessens...@belgium.ZZagfa.com
Mailheader was forged  to fight spam !
Use the above and remove ZZ
______________________________________________

Other Threads