如何释放 Delphi Prism 中对象的数组列表?

发布于 2024-12-03 07:54:48 字数 775 浏览 6 评论 0原文

我需要释放存储在 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 技术交流群。

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

发布评论

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

评论(3

月野兔 2024-12-10 07:54:48

由于您的类没有占用任何非托管资源,例如文件、窗口句柄、数据库连接等。除了让 .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.

呆头 2024-12-10 07:54:48
while theArrayList.count > 0 do
  theArrayList.RemoveAt(0);

GC会帮助你。

while theArrayList.count > 0 do
  theArrayList.RemoveAt(0);

GC will help you.

撧情箌佬 2024-12-10 07:54:48

Delphi Prism 程序在.NET 上运行。不需要释放任何对象,因为垃圾收集器最终会这样做。正如有人已经评论过的那样,如果对象实现了它,您可以调用 IDisposable.Dispose() 来释放内存以外的其他资源。

还有 using 结构,有点像 Delphi 中的 Create-try-finally-Free-end :

using MyArrayList = new ArrayList do
begin
  // use ArrayList...
end; // IDisposable(ArrayList).Dispose is called, if applicable.

当然,这不适用于数组中的项目。如果您确实需要,可以对每个对象调用 Dispose。但一般来说,这是没有必要的。

所以:

method TheForm.DeleteFromList;
begin
  theArrayList.Clear;
end;

不需要释放任何东西。

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:

using MyArrayList = new ArrayList do
begin
  // use ArrayList...
end; // IDisposable(ArrayList).Dispose is called, if applicable.

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:

method TheForm.DeleteFromList;
begin
  theArrayList.Clear;
end;

No need to free anything.

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