Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Removing items from TList

Posted on 1999-07-14
1
Medium Priority
?
197 Views
Last Modified: 2010-04-06
I have been using this kind of method to remove items from the list (mylist: TList;):

function RemoveItem(index: integer): boolean;
var
   p: myclass;
Begin
  Result := False;
    if (index < mylist.Count) and (index >= 0) then
      Begin
        p := mylist[index];
      mylist.delete(index);
         if Assigned(p) then
           Begin
             p.Free;
             p := nil;
           End;
        Result := True;
      End;
End;

Now I read about pack and capacity:
"Call Pack to move all non-nil items to the front of the Items array and reduce the Count property to the number of items actually used. To free up the memory for the unused entries removed by Pack, set the Capacity property to the new value of Count."

So I could use something like this:

mylist.delete(index);
mylist.pack;
mylist.capacity := mylist.count;

How does capacity free up the memory allocated for objecst (of whichs pointers I have in mylist) if I have already deleted the pointer from the list? Or does capacity only free pointers, but not objecst?

Then I was looking for a way to remove pointers from the list when I don't know the index and I came up with this:
// is var needed?
function RemovePointer(var p: myclass): boolean;
Begin
  Result := False;
  if Assigned(p) then
    Begin
     mylist.Remove(p);
     p.Free;
     Result := True;
    End;
End;

These are the ways I've been using. I would like to hear about better/professional ways. I'm using Delphi 4 Standard version.
0
Comment
Question by:tonitop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 200 total points
ID: 1388631
TList never frees the memory for the objects it points to - you must do this (which your remove method does).

The memory the help is referring to is the memory occupied by th pointers to the objects. (ie: TList is just like an array of pointers to things).

If you don't know the index of a pointer use the delete method:

MyList.Delete(p);


Cheers,

Raymond.



0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question