Board index » delphi » MDI-Child Form not set to nil after Closed

MDI-Child Form not set to nil after Closed

In my OnClose event handler methode, I've set the parameter CloseAction to
caFree. In my main form, I check if the child exist (if Assigned(Child) then
...) before creating and using it. I was surprised to see that after closing
the child, it wasn't set to nil: so the main form doesn't create it and it
result in an exception. What I'm doing wrong ??

Thanks.

 

Re:MDI-Child Form not set to nil after Closed


Freeing the object and niling the variable(s) pointing to that object are two
seperate operations.  If you want to have only one instance and that instance
will always point to the Child variable then set the Child variable to nil in
the OnClose event also.  Be vary careful that all variables that point to that
instance are nil'd out.  Usually it is better to rely on FindComponent passing
the name of the form to the MDI Parent if that returns nil then the form does
not exist otherwise it will return the form instance.  Something like

  if FindComponent('Form2') = nil then
    TForm2.Create(self).Show;

Remember that more than one variable can point to the Object.  An object does
not keep track of who all points to it so it can not nil out those variables
when it gets freed.

Quote
Synetrum wrote:

> In my OnClose event handler methode, I've set the parameter CloseAction to
> caFree. In my main form, I check if the child exist (if Assigned(Child) then
> ...) before creating and using it. I was surprised to see that after closing
> the child, it wasn't set to nil: so the main form doesn't create it and it
> result in an exception. What I'm doing wrong ??

> Thanks.

--
Jeff Overcash (TeamB)
      (Please do not email me directly unless  asked. Thank You)
And so I patrol in the valley of the shadow of the tricolor
I must fear evil. For I am but mortal and mortals can only die.
Asking questions, pleading answers from the nameless
faceless watchers that stalk the carpetted  corridors of Whitehall.
             (Fish)
--

Re:MDI-Child Form not set to nil after Closed


On Fri, 2 Jul 1999 13:34:37 +0200, "Synetrum" <synet...@sesamnet.ch>
sent into the ether:

Quote
>In my OnClose event handler methode, I've set the parameter CloseAction to
>caFree. In my main form, I check if the child exist (if Assigned(Child) then
>...) before creating and using it. I was surprised to see that after closing
>the child, it wasn't set to nil: so the main form doesn't create it and it
>result in an exception. What I'm doing wrong ??

Jeff's right, closing the form and setting the form variable to nil
are two separate tasks, and setting the form variable to nil is never
a default action of the VCL.

I differ tho with the recommendation to put the set to nil in
FormClose(). Reason - you *may* sometimes have forms that you want
hidden and reopenable later (using caHide as a return value to
FormClose().) FormClose() will fire whether the form is freed or
hidden. If the form was hidden, you need that same form variable to
stay valid so that you can show the form again.

Instead, I recommend always setting the form variable to nil in
FormDestroy in all your code, in every form. If you do this, the form
variable is guaranteed to be an accurate reflection of the form's
existence at all times - because FormDestroy() is called when the
memory is freed, and at no other time.

IE: consider

procedure TForm1.FormClose(Sender : TObject;Action : ...);
Begin
  Action := caHide; // doesn't delete the form caller
End;

procedure TForm1.FormDestroy(Sender : TObject);
Begin
  Form1 := nil;
End;

procedure ShowFormAgain;
Begin
  if  Assigned(form1) and not form1.Visible then
    form1.Show;
End;

- Don

Re:MDI-Child Form not set to nil after Closed


Argh. Should have read the original post more closely.

MDI children *can't* be hidden, right? So in the one instance of MDI
children being closed (meaning destroyed), FormClose() is as good a
place as any to put the form nil assignment.

Other Threads