Hello I have a list of type TList
. I fill it with pointer to records which are created with new
.
One of my coworkers told me that freeing the list will free all elements, but I have my doubts because I'm use to C. So does Delphi 7 have some sort of garbage collection and I really don't have to free each element? Can someone explain to me how that works?
You only ever need to free what you manually malloc() . So no, depending on the pointer it might not, and might not even be necessary.
You can't free part of an array - you can only free() a pointer that you got from malloc() and when you do that, you'll free all of the allocation you asked for. As far as negative or non-zero-based indices, you can do whatever you want with the pointer when you get it back from malloc() .
TList
holds pointers, but it does not own the things they point at. It cannot, because it has no idea how you allocated them, so it cannot know how to release them, either. You need to destroy those items yourself, if they're supposed to be destroyed.
Your colleague might be thinking of TObjectList
, which can optionally own the items in the list.
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