Solved

Freeing a dynamically created non-modal form.

Posted on 2011-09-14
4
1,771 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
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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