本文介绍了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?

推荐答案

属性 TStringList True ,以便列表在被销毁时释放其对象。这可以通过调用,或通过在创建后显式设置属性。最好将 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.

描述了这样的属性:

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

09-27 00:37