Is it possible for TStringList to automatically free the objects attached to its items?
delphi
Solution
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.
Problem
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?