Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi Remove an object from a TObjectList

I have a TObject list (FileEventObjects := TObjectList.Create(True);) containing one or more objects. The objects need to stay in the list until they are processed. (The object list exists for the duration of the application.)

I'm not entirely sure how to remove a processed object from the list.

Will the object be 'freed' if I do FileEventObjects.Delete(i)

Are there any links to useful examples of TObjectLists in action?

Regards, Pieter.

like image 764
Pieter van Wyk Avatar asked Jul 01 '11 12:07

Pieter van Wyk


2 Answers

If you pass True to the TObjectList constructor (it is also True by default), the list frees any object as soon as you remove it from the collection, no matter if you use Delete, Remove or Clear.

Apart from this, TObjectList can be used just like TList.

like image 153
jpfollenius Avatar answered Oct 06 '22 05:10

jpfollenius


always remember to loop backwards like

for i := Pred(objectlist.Count) downto 0 do
begin
  objectlist.items[i].process;
  objectlist.delete(i);
end;

if you loop from 0 to count -1 whilst removing items you will get access violations

like image 33
Mike Taylor Avatar answered Oct 06 '22 06:10

Mike Taylor