AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Free Objects in List

Ein Thema von Delphi-Lover · begonnen am 18. Apr 2007 · letzter Beitrag vom 18. Apr 2007
Antwort Antwort
Delphi-Lover

Registriert seit: 19. Okt 2004
Ort: Amsterdam
30 Beiträge
 
Delphi 2005 Professional
 
#1

Free Objects in List

  Alt 18. Apr 2007, 14:05
Hello,

I just want to be sure about a thing I wonder about.
Normally to free objects added to a list I do the following:

Delphi-Quellcode:
//The object I add to the List
TMyListObject = Class(TObject)
        ID : Integer;
        Name : String;
End;

//The List
MyList : TStringlist;

//Add the Object in the list
Var aMyListObject : TMyListObject;

aMyListObject:=TMyListObject.Create;
aMyListObject.ID=1;
aMyListObject.Name='Example';

MyList.AddObject('1',aMyListObject);
Normally When I want to free the objects in the list I cast the ListObject in the Free Like:

Delphi-Quellcode:
Var Index : Integer;
Begin
  For Index:=0 to MyList.Count-1 do
  Begin
    TMyListObject(MyList.Objects[Index]).Free;
  End;
  MyList.Clear;
End;
Now I hear that casting is not needed and you can do it directly by the list.objects.free like:

Delphi-Quellcode:
var Index : Integer;
Begin
  for Index := 0 to MyList.Count - 1 do
  begin
    if Assigned(MyList.Objects[Index]) then
    begin
      MyList.Objects[Index].Free;
      MyList.Objects[Index]:=nil;
    end;
  end;
End;
Note, there is no destructor in the ListObject.
I thought you have to cast the listobject while freeing the List, but maybe this is not needed.

Thanks !

Delphi-Lover
Rob
  Mit Zitat antworten Zitat
Udontknow

Registriert seit: 17. Jun 2002
223 Beiträge
 
#2

Re: Free Objects in List

  Alt 18. Apr 2007, 14:18
Hello there!

As the method "Free" is implemented nonvirtual in the main ancestor class TObject, a call of "TListObject(MyObject).Free" is always equal to "MyObject.Free" and will cause a call to the destructor "Destroy". If you dont override the destructor of a descendant class, there is still the destructor "TObject.Destroy" that is inherited. Every object has at least this destructor.

Cu,
Udontknow
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:38 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz