Board index » delphi » Deleting TList objects from TList (D1)

Deleting TList objects from TList (D1)

I wonder what happens if TList has other TList objects as its
items and an item is deleted? Is TList which is under
other TList freed? How about when whole TList is freed?

For example

var
  mainlist: TList;
  itemlist: TList;
begin
  itemlist.Create;
  itemlist.Add(...)
  mainlist.Add(itemlist);

  itemlist.Create;
  itemlist.Add(...)
  mainlist.Add(itemlist);

  mainlist.Delete(0);

  mainlist.Free;
end;

-Timo

 

Re:Deleting TList objects from TList (D1)


On Fri, 30 May 1997 10:44:39 -0700, Timo Romppanen

Quote
<timo.romppa...@upm10.upm-kymmene.com> wrote:
>I wonder what happens if TList has other TList objects as its
>items and an item is deleted? Is TList which is under
>other TList freed? How about when whole TList is freed?

>For example

>var
>  mainlist: TList;
>  itemlist: TList;
>begin
>  itemlist.Create;
>  itemlist.Add(...)
>  mainlist.Add(itemlist);

>  itemlist.Create;
>  itemlist.Add(...)
>  mainlist.Add(itemlist);

>  mainlist.Delete(0);

>  mainlist.Free;
>end;

A TList is just a list of pointers, and it does not delete the objects
in each item when it is deleted.
In your example you will have a memory leak. What you could do is making
a descendant of TList that frees its items upon destruction and use it
for both the mainlist and the itemlists.

Geir

--
Geir Tutturen
geir.tuttu...@itf.nlh.no
(use this address (the return address is a spam avoiding fake))

Re:Deleting TList objects from TList (D1)


Quote
Timo Romppanen wrote:

> I wonder what happens if TList has other TList objects as its
> items and an item is deleted? Is TList which is under
> other TList freed? How about when whole TList is freed?

        Deleting an object from a TList does not free the object,
it just removes its association with the list. This is true for
any sort of object in the list, including a TList object.

--
David Ullrich

?his ?s ?avid ?llrich's ?ig ?ile
(Someone undeleted it for me...)

Other Threads