Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for TStringList to automatically free the objects attached to its items?

Tags:

delphi

I am adding some TObject descendants to a TStringList, for example by calling AddObject. I would like them to be freed when I free the list object. Is there any way to achieve this?

like image 486
iMan Biglari Avatar asked Feb 18 '13 12:02

iMan Biglari


1 Answers

The OwnsObjects property of TStringList should be set to True in order for the list to free its objects when being destroyed. This can be achieved either by calling the constructor overload that receives the OwnsObjects parameter, or by explicitly setting the property after creation. It is preferable to set OwnsObjects as part of the object's construction.

The documentation describes the property like this:

The OwnsObjects property specifies whether the string list owns the stored objects or not. If the OwnsObjects property is set to True, then the Destroy destructor will free up the memory allocated for those objects.

like image 173
iMan Biglari Avatar answered Sep 23 '22 01:09

iMan Biglari