Link to home
Start Free TrialLog in
Avatar of Lester_Clayton
Lester_Clayton

asked on

Freeing a dynamically created non-modal form.

Hello wonderful people - this may seem like a bit of a "noob" question, and I apologize for asking it, but it's not something that's easy to Google, and I've not learned this particular thing.

I've got a Main Application, and It is creating a dynamic form, which is non-modal.  This means that the procedure which is creating the form is not getting an opportunity to free it once it is closed.

What's the safest way to free this form once it's been closed?  Right now, on the dynamically created form, I have a OnFormClose event which has a statement Sender.Free;

I'm guessing that's the right thing to do but I'm not sure how to verify that it is.  The two blocks of code show what I mean.

Thanks for reading.
procedure TMainForm.AnotherFormExecute(Sender: TObject);
Var
  AnotherForm : TAnotherForm;
begin
AnotherForm := TAnotherForm.Create(nil);
AnotherForm.Show;
end;

Open in new window

procedure TAnotherForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Sender.Free;
end;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of VahaC
VahaC
Flag of Ukraine image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Emmanuel PASQUIER
What's the safest way to free this form once it's been closed?  Right now, on the dynamically created form, I have a OnFormClose event which has a statement Sender.Free;

About this : no no no no no , never do that : freeing an object from one of its own event. Most of the time, the 'self' object used by the method calling the event and Sender are the same, or when it's not the case the caller is performing operation with the Sender and will most likely do some again after the event. So if you free the Sender in the event, your application will crash with Access Violation or worse.
Also consider that something triggered the form to close (like a button click), and that some other code might be following that Close action, that are expecting the form (and the components, like the button clicked) to be still around.

It just work in your case because you are lucky DoClose method that is calling the event is probably the last thing done in the chain of treatments started since the root event (clic on [x] for example). Implementation of Close, calling DoClose, when received Action = caFree (VahaC solution, which is right by the way) is to POST a message to destroy the form, so that the actual release is SURE to be executed independently of any action previous the closing request.
procedure TCustomForm.Close;
begin
...
 DoClose(CloseAction);
...
 if CloseAction=caFree Then Release;
end;

procedure TCustomForm.Release;
begin
  PostMessage(Handle, CM_RELEASE, 0, 0); // Post message to queue for delayed processing
end;

procedure CMRelease(var Message: TMessage); message CM_RELEASE;
begin
  Free; // Finally, the form is freed...  but safely :o)
end;

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Lester_Clayton
Lester_Clayton

ASKER

Thanks again VahaC - seems like you're going to become my Delphi friend :)