Board index » delphi » Freeing memory after using AddObject method?

Freeing memory after using AddObject method?

Quote
Phil Haselden wrote:

> I hope this is not a stupid question...

> If you use AddChildObject (or AddObject) to add objects to a
> TOutline, when you call TOutline.Clear does it free the memory
> associated with the object?

> If not, what is the best way to go about reclaiming the memory?

> PhilTStringList does not and I bet Toutline doesn't either.

If I add a bunch of objects to TList or TStringList and I haven't
created a derived type from either of the two, I have to use a typecast
to free the object.

For TStringList I use for example.

 for Index := 0 to StringList.Count - 1 do
TAddedObject(StringList.Objects[Index]).Free;

or for Tlist

 for Index := 0 to List.Count - 1 do
TAddedObject(List.Items[Index]).Free;

This assumes all the add objects are of the same type.

Mark Bracey

 

Re:Freeing memory after using AddObject method?


I hope this is not a stupid question...

If you use AddChildObject (or AddObject) to add objects to a
TOutline, when you call TOutline.Clear does it free the memory
associated with the object?

If not, what is the best way to go about reclaiming the memory?

Phil

Re:Freeing memory after using AddObject method?


Quote
Mark Bracey wrote:

> Phil Haselden wrote:

> > I hope this is not a stupid question...

> > If you use AddChildObject (or AddObject) to add objects to a
> > TOutline, when you call TOutline.Clear does it free the memory
> > associated with the object?

> > If not, what is the best way to go about reclaiming the memory?

> > Phil

> TStringList does not and I bet Toutline doesn't either.

> If I add a bunch of objects to TList or TStringList and I haven't
> created a derived type from either of the two, I have to use a typecast
> to free the object.

> For TStringList I use for example.

>  for Index := 0 to StringList.Count - 1 do
> TAddedObject(StringList.Objects[Index]).Free;

> or for Tlist

>  for Index := 0 to List.Count - 1 do
> TAddedObject(List.Items[Index]).Free;

> This assumes all the add objects are of the same type.

Since all objects derive from TObject, and Destroy is virtual,
you could cast to TObject.

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no

Re:Freeing memory after using AddObject method?


In article 4...@acslink.net.au, Phil Haselden <p...@acslink.net.au> () writes:

Quote
>I hope this is not a stupid question...

>If you use AddChildObject (or AddObject) to add objects to a
>TOutline, when you call TOutline.Clear does it free the memory
>associated with the object?

>If not, what is the best way to go about reclaiming the memory?

>Phil

You have to manually free the memory.  You would do this
by iterating over your list before finally cleraing the
list yourself.  Below is some code (it may have
syntax errors as it is off the top of my head):

  for i := Ouline.Count - 1 downto 0 do
    TMyObject(Outline.Object).Free;
  Outline.Free;

TMyObject is the type of the object added.  It is
also assumed that there is TMyObject.Free method.

Best regards,
Michael Vincze
vin...@ti.com

Re:Freeing memory after using AddObject method?


Quote
Martin Larsson <martin.lars...@delfi-data.msmail.telemax.no> wrote:
>>  for Index := 0 to List.Count - 1 do
>> TAddedObject(List.Items[Index]).Free;

>> This assumes all the add objects are of the same type.

>Since all objects derive from TObject, and Destroy is virtual,
>you could cast to TObject.

Since the list contains TObjects you don't need to cast anyway and
make sure your Destroy overrides, being virtual doesn't imply
overriding.

Cheers Terry...

Re:Freeing memory after using AddObject method?


Quote
Terry Harris wrote:

> Martin Larsson <martin.lars...@delfi-data.msmail.telemax.no> wrote:

> >>  for Index := 0 to List.Count - 1 do
> >> TAddedObject(List.Items[Index]).Free;

> >> This assumes all the add objects are of the same type.

> >Since all objects derive from TObject, and Destroy is virtual,
> >you could cast to TObject.

> Since the list contains TObjects you don't need to cast anyway and

TList does not contain TObjects, just pointers. TStringList
contains TObjects. So, if you're using a TList, you need to
cast.

Quote
> make sure your Destroy overrides, being virtual doesn't imply
> overriding.

I should've been more precise. What I meant is that you don't
need to cast to the kind of object you put in, TObject is fine
(and lets you have several different objects in the list) since
Destroy is virtual. You only need to include it if you need to
free something. But if you do, it should, as you stated, use
the override directive.

M.

--
Martin Larsson, author of several unknown utilities for DOS and Windows.
mailto:martin.lars...@delfi-data.msmail.telemax.no
http://www.delfidata.no/users/~martin
X.400:G=martin;S=larsson;O=delfi-data;P=msmail;A=telemax;C=no

Other Threads