在Delphi中使用Free而不是Release作为模态表单是否安全? [英] Is it safe to use Free instead of Release for modal forms in Delphi?

查看:70
本文介绍了在Delphi中使用Free而不是Release作为模态表单是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Delphi在线帮助说,应使用Release从内存中删除表单.但是,在模态形式的许多示例中,我都看到了这种构造:

The Delphi online help says that Release should be used to remove a form from memory. However, in many examples for modal forms I have seen this construct:

MyForm := TMyForm.Create(nil);
try
  MyForm.ShowModal;
finally
  MyForm.Free;
end;

免费提供销毁模态表格的安全方法吗?正如我在ShowModal的源代码中看到的那样,将调用Application.HandleMessage直到ModalResult不为0.这是Free不能干扰待处理的Windows消息的原因吗?

Is Free a safe way to destroy a modal form? As I can see in the source for ShowModal, Application.HandleMessage will be called until the ModalResult is not 0. Is this the reason why Free can not interfere with pending windows messages?

推荐答案

是的,在ShowModal调用之后使用Free是安全的.

Yes, it's safe to use Free after a ShowModal call.

您需要使用Release的情况是您在事件处理程序(例如,OnClick)中间的时候,该事件之后的进一步处理将必须访问该表单.在那种情况下,调用Release会发布一条CM_RELEASE消息,该消息在事件处理程序完成并且控制权返回到消息泵(ProcessMessages/Application.Run)之前不会释放事件. ShowModal不会返回,直到事件处理程序完成并且控制使其备份堆栈为止,因此事后调用Free实际上是在其他地方处理CM_RELEASE消息的位置.

The cases where you need to use Release are times when you're in the middle of an event handler (eg, OnClick), where further processing after the event will have to access the form. In that case calling Release instead posts a CM_RELEASE message which doesn't free the event until the event handler is done and control has returned to the message pump (ProcessMessages/Application.Run). ShowModal doesn't return until the event handler is finished and control makes it back up the stack, so calling Free afterwards is effectively the same place where the CM_RELEASE message would be processed otherwise.

这篇关于在Delphi中使用Free而不是Release作为模态表单是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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