?
Solved

Freeing a dynamically created non-modal form.

Posted on 2011-09-14
4
Medium Priority
?
1,848 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 800 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 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

770 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