如何释放 Delphi Prism 中对象的数组列表?
我需要释放存储在 ArrayList 中的对象列表。我知道你可以在Delphi中调用Free procedure,但是在Delphi Prism中没有free procedure。我不仅想从列表中删除对象,还想将其从内存中释放。
例如,假设我有以下类
TheClass = Class
private
theStr:String;
protected
public
end;
method TheForm;
begin
TheArrayList:=new ArrayList;
end;
要添加对象,我会这样做:
method TheForm.AddToList;
var
tmpObj:TheClass;
begin
tmpObj := new TheClass;
TheArrayList.Add(tmpObj);
end;
要从列表中删除对象,这就是我会这样做的方法,但没有免费的 程序。
method TheForm.DeleteFromList;
var I:integer;
begin
for I:=0 to theArrayList.count-1 do
begin
theClass(theArrayList[I]).free; <-------I know this doesnt work.
theArrayList.RemoveAt(I);
end;
end;
end;
Delphi Prism 中如何释放对象列表?
谢谢,
I need to free list of objects stored in an ArrayList. I know you can call Free procedure in Delphi, but in Delphi Prism there is no free procedure. I don't just want to remove objects from the list but also free it from its memory.
For instance Say I have this following class
TheClass = Class
private
theStr:String;
protected
public
end;
method TheForm;
begin
TheArrayList:=new ArrayList;
end;
To Add Object I would do this:
method TheForm.AddToList;
var
tmpObj:TheClass;
begin
tmpObj := new TheClass;
TheArrayList.Add(tmpObj);
end;
To Delete Object from the list, this is how I would do it but there is no free
procedure.
method TheForm.DeleteFromList;
var I:integer;
begin
for I:=0 to theArrayList.count-1 do
begin
theClass(theArrayList[I]).free; <-------I know this doesnt work.
theArrayList.RemoveAt(I);
end;
end;
end;
How is freeing list of object accomplished in Delphi Prism?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您的类没有占用任何非托管资源,例如文件、窗口句柄、数据库连接等。除了让 .net 垃圾收集器在认为时机成熟时释放内存之外,您无需执行任何操作。
尝试强制垃圾收集器提前运行通常会导致性能比简单地让它完成工作更差。
如果您有一个包含非托管资源的类,那么您应该遵循 IDisposable 模式。
Since your class is not holding onto any unmanaged resources like files, window handles, database connections etc. you need do nothing beyond letting the .net garbage collector free the memory when it decides the time is right.
Trying to force the garbage collector to run ahead of time typically leads to worse performance than simply letting it do its job.
If you had a class with unmanaged resources then you should follow the IDisposable pattern.
GC会帮助你。
GC will help you.
Delphi Prism 程序在.NET 上运行。不需要释放任何对象,因为垃圾收集器最终会这样做。正如有人已经评论过的那样,如果对象实现了它,您可以调用 IDisposable.Dispose() 来释放内存以外的其他资源。
还有 using 结构,有点像 Delphi 中的 Create-try-finally-Free-end :
当然,这不适用于数组中的项目。如果您确实需要,可以对每个对象调用 Dispose。但一般来说,这是没有必要的。
所以:
不需要释放任何东西。
Delphi Prism programs run on .NET. There is no need to free any objects, since the garbage colleector will eventually do that. As someone already commented, you can call IDisposable.Dispose() to free other resources than memory, if the object implements it.
There is also the using construct, which is a bit like Create-try-finally-Free-end in Delphi:
This won't work for the items in the array, of course. If you really want, you can call Dispose on each of them. But generally, this is not necessary.
So:
No need to free anything.