title: How can I check whether an object reference is still valid?
body: <p>I have an issues where I am trying to determine if a reference to an object is valid.  But it seems to be returning strange results.</p>
  
  <pre><code>procedure TForm1.Button1Click(Sender: TObject);
    var form1 : TForm;
        ref2 : TControl;
  begin
    form1 := TForm.Create(nil);
    form1.Name := 'CustomForm';
    form1.Parent := self; //Main Form
  
    form1.Show;
  
    ref2 := form1;
    showmessage(ref2.ClassName+' - '+ref2.Name+' - '+BoolToStr(ref2.visible,true));
    freeandnil(form1);
    showmessage(ref2.ClassName+' - '+ref2.Name+' - '+BoolToStr(ref2.visible,true));
  end;
  </code></pre>
  
  <p>The first showmessage returns - "TForm - CustomForm - True" (Just like I would expect it to).</p>
  
  <p>The second showmessage return - "TForm - - False".  I was actually hoping for some kind of access violation that I could then trap and know that the reference isn't valid.  </p>
  
  <p>In my application I need to compile a list of random TForm descendants as they are created and then check later if they have gone away (or are not visible).  Unfortunately it is a plugin based system so I can go change all of these Forms to post a "I'm done Message."</p>
  
  <p>Would code like this be safe to use (assuming I actually am checking for access violations)?  Does anybody have any ideas what is happening.  </p>
  
  <p>Thanks</p>