Board index » delphi » How do TList.Delete and TList.Free work

How do TList.Delete and TList.Free work

Does Delphi's TList Free the objects it points to?

var
        AList: TList ;
begin
        AList := TList.Create ;

        AList.Add( MyObject.Create( 1) ) ;      
        AList.Add( MyObject.Create( 2 ) ) ;    

        AList.Delete( 1 ) ;
        AList.Free ;
end ;

Is MyObject( 1 ) Freeed from memory, or pardon my c++, deleted? And does
the call to AList.Free free (delete) MyObject( 2 ).

Thanks in advance

Ashley

 

Re:How do TList.Delete and TList.Free work


Quote
ash...@u030.aone.net.au (Ashley{*word*106}son) wrote:
>Does Delphi's TList Free the objects it points to?
>var
>    AList: TList ;
>begin
>    AList := TList.Create ;
>    AList.Add( MyObject.Create( 1) ) ;      
>    AList.Add( MyObject.Create( 2 ) ) ;    
>    AList.Delete( 1 ) ;
>    AList.Free ;
>end ;
>Is MyObject( 1 ) Freeed from memory, or pardon my c++, deleted? And does
>the call to AList.Free free (delete) MyObject( 2 ).
>Thanks in advance
>Ashley

I'm not sure about Delphi, but in Object Pascal Delete method of
Collection just deletes item from it without disposing the memory and
Free method dispose item and delete it... So IMHO MyObject(1) isn't
deleted from heap - you got lost pointer :)

Cheers
  Dmitri

--------------------------------------------------------------
        Shit happens, but why it always happens to us?
--------------------------------------------------------------
Dmitri Poujlivyi            mailto:  dmi...@god.bel.alcatel.be
                            http://www.dma.be/p/bewoner/dmitri

Re:How do TList.Delete and TList.Free work


In article <531h2b$...@news.mel.aone.net.au>,
ash...@u030.aone.net.au (Ashley{*word*106}son) says:

Quote
>Does Delphi's TList Free the objects it points to?

No. You must free each item in the list.
Both the Delete method and the Free destructor remove items
from the list, without destroying them.

So, you shouldn't have to worry about items being in multiple
lists at the same time.
It's a good idea to have a master list with all created items,
and destroy all items in this list.

Note that the old TCollection destructor DID destroy all
items!

Quote
>var
>        AList: TList ;
>begin
>        AList := TList.Create ;

>        AList.Add( MyObject.Create( 1) ) ;      
>        AList.Add( MyObject.Create( 2 ) ) ;    

>        AList.Delete( 1 ) ;
>        AList.Free ;
>end ;

>Is MyObject( 1 ) Freeed from memory, or pardon my c++, deleted? And does
>the call to AList.Free free (delete) MyObject( 2 ).

If you're unsure, set a breakpoint at the destructor of MyObject
and find out!

-----------------------------------------------------------------------
  Zweitze de Vries                           zwei...@cyco.nl
  Cyco Software Development
  Handelskade 49
  Rijswijk, Netherlands                      (+31) 70 3954179

Other Threads