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

解决方案 »

  1.   

    Form?
    if Application.FindComponent('formName') <> nil then 
    showmessage('存在')
    else
    showmessage('不存在');
      

  2.   

    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所指内存收回而导致出错。 
      

  3.   

      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')
      

  4.   

    if Assigned(nForm) then
    begin
      ShowMessage("nForm 不为 nil");
      try
        nForm.Visible := false;
        ShowMessage("nForm 没有 Free");
      except
        ShowMessage("nForm 已经 Free");
      end;
    end; 这样也算吧?
      

  5.   

    楼上的朋友通过调用nForm的属性 nForm.Visible  是否抛出异常来判断nForm是否已经被free。
    这个方法不错,
    还有没有别的方法?
      

  6.   

    折腾这个干啥?直接用freeandnil不就得了?
      

  7.   

    if Assigned(Form1) then
    就是判断是否为空的:function Assigned(const P): Boolean;
      form := TForm.Create(nil);   
      form.ShowModal;
      form.Free;  {3}   
      //form := nil;  {1}   
      //FreeAndNil(form);  {2}   
      if Assigned(form) then     
        ShowMessage('不为空!'); {执行这行代码。因为只是释放了内存,指针并没有置空}
      {其他的就不举例了,自己琢磨}   
      

  8.   


    for i := 0 to Self.MDIChildCount do
      begin
        if Self.MDIChildren[I] = nform then
        begin
         ShowMessage('存在');
         break;
        end;
      end;
    对于MDI子窗口。