The VCL already has a mechanism to notify components when other components are freed. You can use it this way;
type
TfrmParent = class(TForm)
btnShowChild: TButton;
procedure btnShowChildClick(Sender: TObject);
private
FChild: TfrmChild;
public
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
end;
procedure TfrmParent.btnShowChildClick(Sender: TObject);
begin
// Check status of child
if FChild = nil then
begin
// Child does not exist, create it
FChild:= TfrmChild.Create(Application);
FChild.Show;
// Ask Child to notify us when it is destroyed
FChild.FreeNotification(Self);
end
else
begin
// Child already exists
FChild.Show;
end;
end;
procedure TfrmParent.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited;
if (AComponent = FChild) and (Operation = opRemove) then
begin
// FChild is about to be freed, so set reference to Child to nil
FChild:= nil;
end;
end;
After creating the child form, use the created form's FreeNotification method to register yourself to receive a notification when the child form is destroyed.
To react to the notification, overwrite the Notification method. In there, you can find out which component is destroyed and compare it to the remembered reference to the child form. When you receive the notification, just set the reference to the child form to nil.
In the child TfrmChild itself you don't have to do anything else but what skamradt has written: Just set the parameter Actionb to caFree in the OnClose event.