Solved

Freeing a dynamically created non-modal form.

Posted on 2011-09-14
4
1,805 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:Lester_Clayton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Accepted Solution

by:
VahaC earned 200 total points
ID: 36538792
procedure TAnotherForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

Open in new window

Delphi help
caNone
 The form is not allowed to close, so nothing happens.
 
caHide
 The form is not closed, but just hidden. Your application can still access a hidden form.
 
caFree
 The form is closed and all allocated memory for the form is freed.
 
caMinimize
 The form is minimized, rather than closed.
 
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36539705
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

0
 
LVL 19

Assisted Solution

by:Thommy
Thommy earned 50 total points
ID: 36540926
"Action := caFree" in FormClose-event ist the right solution!!!

Some more info about freeing non-model forms...

How to close non-modal form in Delphi
http://stackoverflow.com/questions/2075405/how-to-close-non-modal-form-in-delphi

Making Forms Work - a Primer
http://delphi.about.com/od/beginners/l/aa070203c.htm
0
 
LVL 9

Author Closing Comment

by:Lester_Clayton
ID: 36542017
Thanks again VahaC - seems like you're going to become my Delphi friend :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question