读书人

怎么判断nForm已经free但还没有nil的状

发布时间: 2012-04-21 14:34:44 作者: rapoo

如何判断nForm已经free但还没有nil的状态
如何判断nForm已经free但还没有nil的状态

[解决办法]
Form?
if Application.FindComponent('formName') <> nil then
showmessage('存在')
else
showmessage('不存在');
[解决办法]
if not assigned(form) then //没有分配
form.free 销毁对象
from.close 关闭窗体
form:=nil 指针赋值
在delphi中,所有vcl对象都是指针,你所说的form也是一个指针
form.free:
收回form指针所指对象分配的内存(调用tform的析构函数释放窗口对象);
from.close:
关闭窗体,from所指的对象并未释放内存,因此还是可以访问的,例如form.name,等等;
form:=nil:
将指针指向空,但分配的内存未释放,这样做会导致内存泄漏。

最好将Form定义为全局变量而非局部变量,因为局部变量超过作用域后指针变量就不可访问了,但分配的对象内存仍然存在,将导致内存泄漏;
收回动态创建的窗口,最好使用FreeAndNil(Form)而不是简单的使用free,便不能用destroy,这样的好处是,由于程序逻辑的需要要再次访问Form时,不会因为form所指内存收回而导致出错。
[解决办法]
B := False;
for I := 0 to Screen.FormCount - 1 do
begin
if Screen.Forms[I] = Form2 then
begin
B := True;
Break
end
end;
if (not B) and (Form2 <> nil) then
ShowMessage('已经free但还没有nil')
[解决办法]

Delphi(Pascal) code
if Assigned(nForm) thenbegin  ShowMessage("nForm 不为 nil");  try    nForm.Visible := false;    ShowMessage("nForm 没有 Free");  except    ShowMessage("nForm 已经 Free");  end;end;
[解决办法]
showmodal
[解决办法]
折腾这个干啥?直接用freeandnil不就得了?
[解决办法]
if Assigned(Form1) then
就是判断是否为空的:function Assigned(const P): Boolean;

Delphi(Pascal) code
  form := TForm.Create(nil);     form.ShowModal;  form.Free;  {3}     //form := nil;  {1}     //FreeAndNil(form);  {2}     if Assigned(form) then         ShowMessage('不为空!'); {执行这行代码。因为只是释放了内存,指针并没有置空}  {其他的就不举例了,自己琢磨}
[解决办法]
Delphi(Pascal) code
for i := 0 to Self.MDIChildCount do  begin    if Self.MDIChildren[I] = nform then    begin     ShowMessage('存在');     break;    end;  end; 

读书人网 >.NET

热点推荐