Delphi Remove an object from a TObjectList

Pieter van Wyk picture Pieter van Wyk · Jul 1, 2011 · Viewed 13.5k times · Source

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.

Answer

jpfollenius picture jpfollenius · Jul 1, 2011

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.