Solved

Freeing a dynamically created non-modal form.

Posted on 2011-09-14
4
1,698 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
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Comment Utility
Thanks again VahaC - seems like you're going to become my Delphi friend :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now