Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to free elements of a list?

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?

like image 286
Earlz Avatar asked Oct 19 '11 19:10

Earlz


People also ask

Do arrays need to be freed C?

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.

How do you free elements of an array?

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() .


1 Answers

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.

like image 103
Rob Kennedy Avatar answered Nov 03 '22 00:11

Rob Kennedy