TStringList是否可以自动释放附加到其项目的对象? [英] Is it possible for TStringList to automatically free the objects attached to its items?

查看:191
本文介绍了TStringList是否可以自动释放附加到其项目的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些 TObject 后代添加到 TStringList 中,例如通过调用 AddObject 。当我释放列表对象时,我希望它们被释放。有没有办法实现这一点?

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?

推荐答案

OwnsObjects 属性 TStringList True ,以便列表在被销毁时释放其对象。这可以通过调用收到<$ c $的构造函数重载来实现c> OwnsObjects 参数,或通过在创建后显式设置属性。最好将 OwnsObjects 设置为对象构造的一部分。

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.

文档描述了这样的属性:


OwnsObjects 属性指定字符串列表是否拥有存储的对象。如果 OwnsObjects 属性设置为True,则Destroy析构函数将释放为这些对象分配的内存。

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.

这篇关于TStringList是否可以自动释放附加到其项目的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆