Delphi:表单可以自行释放吗?

发布于 2024-07-16 02:02:33 字数 95 浏览 10 评论 0原文

我有一个表格,用来显示一些信息几秒钟。 表单可以自行释放吗? 我可以在构造函数中启动一个计时器,然后在计时器事件中调用 self.free 吗? 或者这可能会带来麻烦吗?

I have a form that I use to show some information for some seconds. Is it ok for the form to free itself? Can I start a timer in the constructor, and then call self.free in the timer-event? Or will this potentially lead to trouble?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

琉璃繁缕 2024-07-23 02:02:33

此外,通过表单您可以调用Release

它向表单发送一条 CM_RELEASE 消息。 作为反应,它称之为“免费”。
发布的优点是不会为表单留下任何可能导致崩溃的消息。

In addition, with a form you can call Release.

It sends a CM_RELEASE message to the form. As a reaction it calls Free.
The advantage of release is that there are no messages left for the form which could result in a crash.

玩套路吗 2024-07-23 02:02:33

您可以使表单在被用户或代码关闭时释放自身:

procedure TForm27.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TForm27.FormCreate(Sender: TObject);
begin
  Timer1.Enabled := True;
end;

procedure TForm27.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;
  Close;
end;

确保在构造函数中提供所有者,以防应用程序关闭并且表单当时没有被销毁。 所有者将在释放自身之前释放该表单。

You can make the form to free itself when it gets closed by the user or from code:

procedure TForm27.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TForm27.FormCreate(Sender: TObject);
begin
  Timer1.Enabled := True;
end;

procedure TForm27.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;
  Close;
end;

Make sure you supply an owner in the constructor incase the application shutdowns and the form is not destroyed at the time. The owner will free the form before freeing itself.

笑饮青盏花 2024-07-23 02:02:33

我有一整套可以自行释放的对象,并且我对它们进行了各种测试,没有显示任何问题/泄漏。 TForm 可能更复杂,但只要 Self.Free() 是最后一次调用,您就应该是安全的。

(对于那些想知道为什么我到底有可以释放自己的对象的人;我在系统中传递了很多它们,所以我通过自己的引用计数方案来实现。当最后一个引用被释放时,所以对象会释放自己)。

I have a whole suite of objects that free themselves, and I've run various tests on them with no problems/leaks shown. A TForm might be more complex, but as long as Self.Free() is the last call made, you ought to be safe.

(For those wondering why on earth I have object that free themselves; I pass them around the system a lot, so I implemented by own reference counting scheme. When the last reference is released, so the object frees itself).

浮萍、无处依 2024-07-23 02:02:33

这正是接口所做的事情。

This is exactly what is done with Interfaces.

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