如何在Delphi 7析构函数中释放一组对象? [英] How should I free an array of objects in a Delphi 7 destructor?

查看:134
本文介绍了如何在Delphi 7析构函数中释放一组对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的Delphi类看起来像这样:

Suppose my Delphi classes look like this:

interface
type

    TMySubInfo = class
    public
        Name : string;
        Date : TDateTime;
        Age  : Integer;
    end;

    TMyInfo = class
    public
        Name : string;
        SubInfo : array of TMySubInfo;
        destructor Destroy; override;
    end;

implementation

    destructor TMyInfo.Destroy;
    begin
      // hmmm..
    end;

end.

要正确清理,析构函数应该怎么做?是否足够做 SetLength(SubInfo,0),或者我需要循环并释放每个 TMySubInfo ?我需要做任何事情吗?

To properly clean up, what should go in the destructor? Is it enough to do SetLength(SubInfo,0), or do I need to loop through and free each TMySubInfo? Do I need to do anything at all?

推荐答案

你需要循环并释放每个创建的对象。

You need to loop through and free each created object.

您必须知道,声明TMySubInfo数组实际上并不创建对象。您必须稍后再创建它们。

You must know, that declaring a array of TMySubInfo doesn't actually create the objects. You have to create them later on.

我将使用TList来实现更加动态的方法。您甚至可以使用一个TObjectList,当该列表被释放时,它可以释放所有的项目。

I would use a TList instead for a more dynamic approach. You could even use a TObjectList that can free all its items when the list gets freed.

这篇关于如何在Delphi 7析构函数中释放一组对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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