我需要释放列表中的元素吗?
您好,我有一个 TList
类型的列表。我用指向使用 new
创建的记录的指针填充它。
我的一位同事告诉我,释放列表将释放所有元素,但我有疑问,因为我习惯了 C。那么 Delphi 7 是否有某种垃圾收集,我真的不需要释放每个元素?有人可以向我解释它是如何工作的吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TList
保存指针,但它不拥有它们所指向的东西。它不能,因为它不知道你如何分配它们,所以它也不知道如何释放它们。如果这些物品应该被销毁,你需要亲自销毁它们。您的同事可能正在考虑
TObjectList
,它可以选择拥有列表中的项目。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.