我需要释放列表中的元素吗?

发布于 2024-12-11 00:54:02 字数 174 浏览 3 评论 0原文

您好,我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2024-12-18 00:54:03

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文