Delphi Remove object from TObjectList

I have a TObject list ( FileEventObjects := TObjectList.Create(True); ) containing one or more objects. Objects must remain on the list until they are processed. (A list of objects exists throughout the application.)

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

Will the object be β€œfreed” if I do FileEventObjects.Delete(i)

Are there links to useful examples of TObjectLists in action?

Regards, Peter.

+7
source share
2 answers

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

In addition, a TObjectList can be used in the same way as a TList .

+12
source

always remember to cycle back like

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

if you execute a cycle from 0 to count -1, and when deleting elements you will get access violations.

+4
source

All Articles