表格.发布+ 零

发布于 2024-07-08 06:02:55 字数 473 浏览 7 评论 0原文

如果在使用表单后调用 Form.Release,它将释放所有相关内存,但不会将表单变量设置为 nil。

if not assigned (Form1) then
  begin
    Application.CreateForm(Tform1, Form1);
    try
      // Do something
    finally
      Form1.Release
    end;
  end;

为了能够再次调用相同的代码,Form1 必须在某个时刻设置为 nil。 从Release的描述来看,我不能

Form1 := nil;

在Release之后立即执行此操作,因为Release过程将在被调用之后、窗体实际释放之前直接返回。 我无法检测 Form.Release 何时完成以将表单 var 设置为 nil。

做这个的最好方式是什么?

if Form.Release is called after using the form, it will free all related memory but not set the form variable to nil.

if not assigned (Form1) then
  begin
    Application.CreateForm(Tform1, Form1);
    try
      // Do something
    finally
      Form1.Release
    end;
  end;

To be able to call the same code again, Form1 would have to be set to nil at some point. From the description of Release I cannot do

Form1 := nil;

right after Release, because the Release procedure will return directly after being called and before the form is actually freed. I cannot detect when Form.Release is finished to set the form var to nil.

What is the best way to do this?

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

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

发布评论

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

评论(5

雨落星ぅ辰 2024-07-15 06:02:55

将该行放在

  Form1 := nil;  

对 Release 的调用之后。

Release 只是将 CM_RELEASE 消息发布到表单,这允许表单在处理 CM_RELEASE 消息之前完成其队列(事件处理程序)中的内容,这意味着通常只调用 Free。
因此,在调用 Release 之后,您不应该假设 Form 变量仍然指向有效的 Form,从而将 nil 放入该变量中。

Put the line

  Form1 := nil;  

just after the call to Release.

Release is just posting a CM_RELEASE message to the Form which allows the Form to finish what's in its queue (event handlers) before handling the CM_RELEASE message which means normally just calling Free.
So, after calling Release, you should not assume that the Form variable still points to a valid Form, thus putting nil into the variable.

鹿童谣 2024-07-15 06:02:55

发布只是(可能)延迟的免费。 调用 Release 后应该做的第一件事就是将变量清空。
这样,即使某些代码在 Form1 实际被销毁之前尝试引用它,您也将是安全的。 在像您的代码中这样的情况下,它只会安全地重新创建另一个 Form1 以供新用途,而不会打扰被破坏的 Form1。

Release is just a (potentially) deferred Free. The 1st thing you should do after calling Release is nilling the variable.
Then, you'll be safe even if some code tries to reference Form1 before it is actually destroyed. In a case like in your code, it would just safely recreate another Form1 for the new usage without bothering the one being destroyed.

三五鸿雁 2024-07-15 06:02:55

您可以始终这样称呼:

procedure FreeOrReleaseAndNil(var Obj);
var
  Temp: TObject;
begin
  Temp := TObject(Obj);
  Pointer(Obj) := nil;
  if Temp is TCustomForm then
    TCustomForm(Temp).Release
  else
    Temp.Free;
end;

请务必在转换为 TObject 后检查类型,因为您无法测试 Obj 的类型。 这应该是安全的,因为像免费一样,发布是非虚拟的。

Of you could just always call this:

procedure FreeOrReleaseAndNil(var Obj);
var
  Temp: TObject;
begin
  Temp := TObject(Obj);
  Pointer(Obj) := nil;
  if Temp is TCustomForm then
    TCustomForm(Temp).Release
  else
    Temp.Free;
end;

Be sure to check the type after casting to a TObject as you can't test the type of Obj. This should be safe since like Free, Release is non-virtual.

尝蛊 2024-07-15 06:02:55

如前所述,发布只是表单想要关闭/释放自身时使用的延迟释放。 除了被推迟之外,它与发布没有什么不同。 所以在该示例中调用 Release 是没有用的。 调用 Free 似乎更合乎逻辑。 并且可以在调用 Free 后将其设置为 nil 或使用 FreeAndNil。

如果您仍然想使用 Release 也没关系。 只需将变量值设置为 nil 即可。 这样做并不会使论坛的行为有所不同。 但请记住,在这种情况下,调用 Free 而不是 Release 更高效且更具确定性。 我的偏好是只在真正需要的地方使用 Release。

As is mentioned Release is only a deferred Free for a form to use if it wants to Close/Free itself. Other then being deferred it does nothing different from Release. So there is no use in calling Release in that example. Calling Free seems to be more logical. And you can set it to nil after calling Free or use FreeAndNil.

If you still want to use Release that is fine. Just setting the variable value to nil works. Doing that does not make the forum behave differently. But remember that in this case it is more efficient and more deterministic to call Free instead of Release. My preference is to only use Release where is really needed.

祁梦 2024-07-15 06:02:55

在 Delphi Win32 中,释放对象的适当方法是调用

FreeAndNil(Form1)

This 在一次调用中完成这两项工作。

然而,我有一种隐隐的感觉,你的问题比表面上看到的要复杂得多。 您是否使用 Delphi for .NET - 如果是,是哪个版本?

In Delphi Win32, the appropriate way to free objects is to call

FreeAndNil(Form1)

This does both jobs in a single call.

However, I have a sneaking feeling there's more to your question than meets the eye. Are you using Delphi for .NET - and if so, which version?

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