本文介绍了表格发布+ NIL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在使用表单后调用Form.Release,它将释放所有相关的内存,但不会将表单变量设置为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;

要能够再次调用相同的代码,必须将Form1设置为nil 。从发布的描述中我不能做到

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;

在Release之后就可以了,因为Release过程将在调用后且实际释放表单之前直接返回。我无法检测到Form.Release何时完成将var设置为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?

推荐答案

插入行

  Form1 := nil;

紧接在调用Release之后。

just after the call to Release.

Release只是将CM_RELEASE消息发布到Form,这使Form在处理CM_RELEASE消息之前先完成队列中的内容(事件处理程序),这通常意味着仅调用Free。

因此,在调用Release之后,您不应该假设Form变量仍然指向有效的Form,因此将nil放入变量中。

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.

这篇关于表格发布+ NIL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 21:13