Board index » delphi » Help : Memory and records ????

Help : Memory and records ????

Hello again,
                I am using delphi 2.0 and have found that when creating types, filling
them with information, and chucking them into a tlist works quite well
until > you try to destroy or free them ? I know that a tlist only holds a
pointer to the type that I have created and have tryed to pass this to the
destroy and dispose methods but to no avail

any one got any ideas ?

help would be most appriciated (never could spell that word)

Mikey.

 

Re:Help : Memory and records ????


In article <01bc7209$fcb62980$4e89b...@mikey.interalpha.co.uk> "Mikey" <micha...@interalpha.co.uk> writes:

Quote
>Hello again,
>                I am using delphi 2.0 and have found that when creating types, filling
>them with information, and chucking them into a tlist works quite well
>until > you try to destroy or free them ? I know that a tlist only holds a
>pointer to the type that I have created and have tryed to pass this to the
>destroy and dispose methods but to no avail
>any one got any ideas ?
>help would be most appriciated (never could spell that word)

"appreciated."

The guts of a TList is a pointer-array structure, and notwithstanding anything
Borland might have implied in its documentation, throwing away a TList does
*not* throw away any objects that it pointed to.

What you need to do is to iterate through the "objects" array of the list,
disposing of anything it points to and (I recommend...) setting each "objects"
pointer to NIL.  Then you are free to dispose of the list itself.

You should use the "free" method in all cases; not "destroy."

If you allocated the object with "new" you should use "dispose" to free it;
otherwise you should not use "dispose."

Re:Help : Memory and records ????


In article <01bc7209$fcb62980$4e89b...@mikey.interalpha.co.uk>,
   "Mikey" <micha...@interalpha.co.uk> wrote:

Quote
>Hello again,
>            I am using delphi 2.0 and have found that when creating
>types, filling them with information, and chucking them into a tlist
>works quite well until you try to destroy or free them ? I know that a
>tlist only holds a pointer to the type that I have created and have tryed
>to pass this to the destroy and dispose methods but to no avail

TLists are used quite a lot merely to refer to a whole lot of other
things.  One of my personal favorite uses is, when making a new class
based on TComponent (or any of its descendants), to override the
Notification method...

Let's say I wanted to keep track of all my TThingy objects (TThingy also
being based on TComponent)...

Add this to the private section of TMyComponent:

  FThingies : TList;
  function GetThingy(AIndex: Integer): TThingy;

Add this to the protected section:

  procedure Notification(AComponent: TComponent;AOperation: TOperation);
    override;

Add this to the public section

  property Thingy[AIndex: Integer]: TThingy read GetThingy;

For your get, add:

function TMyComponent.GetThingy(AIndex: Integer);
begin
  Result := TThingy(FThingies[AIndex]);
end;

In your constructor, add:

  FThingies := TList.Create;

In your destructor, add:

  FThingies.Free;

In your Notification method:

procedure TMyComponent.Notification(AComponent: TComponent;AOperation:
  TOperation);
begin
  if AComponent is TThingy then
    if Operation = opInsert then
      FThingies.Add(AComponent)
    else
      FThingies.Remove(AComponent);
end;

Now, if you create a TThingy like this:

  MyThingy := TThingy.Create(MyComponentObject);

It automatically adds it to the thingy list in MyComponentObject.
You can then access MyComponentObject.Thingy[0].
Similarly, MyThingy.Free will remove it.

Quote
>any one got any ideas ?

If you're trying to work with the Free and/or Dispose with the items
in a TList, remember that a generic Pointer won't give you much info.

If you've got a class in your TList, you can free it thusly:

  TMyClass(MyList[X]).Free;

If you've got a certain pointer-to-record (for example) type in your
TList, you can free it like this:

  Dispose(TMyRecord(MyList[X]));

(Or FreeMem(TMyRecord(MyList[X]),SizeOfTheRecord) if you used GetMem to
allocate it)

Quote
>help would be most appriciated (never could spell that word)

Hope that helps :)

  --=- Ritchie Annand

Re:Help : Memory and records ????


You should write a small while loop to dispose of the attached objects.

Appreciated

Other Threads