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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With