Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Application.Terminate, OnClose, OnDestroy

753 views
Skip to first unread message

TObject

unread,
May 4, 2003, 5:46:12 PM5/4/03
to
I create a number of secondary forms in an application using

InstantiatedFormObject.Show method. Those objects get
freed by setting the Action := caFree in the OnClose event.

Everything works well, however, I noticed that nether
OnClose nor OnDestroy events for these secondary forms
are fired if I exit the Application by closing the Main Form,
or calling Application.Terminate;

I am using Delphi 5. What am I missing here, and what is
the correct way to trap OnClose and OnDestroy events
for open non-modal forms, when application terminates?

Thanks!

Romulo A. Ceccon

unread,
May 5, 2003, 12:15:27 PM5/5/03
to
TObject wrote:

> Everything works well, however, I noticed that nether
> OnClose nor OnDestroy events for these secondary forms
> are fired if I exit the Application by closing the Main Form,
> or calling Application.Terminate;

You need to set the Owner of your secondary forms to be either
application or the main form. This way, before the application or the
main form gets freed, the code to free your forms will be called.

Romulo

Peter Below (TeamB)

unread,
May 5, 2003, 1:33:29 PM5/5/03
to
In article <3eb58908$1...@newsgroups.borland.com>, TObject wrote:
> I create a number of secondary forms in an application using
> InstantiatedFormObject.Show method. Those objects get
> freed by setting the Action := caFree in the OnClose event.
>
> Everything works well, however, I noticed that nether
> OnClose nor OnDestroy events for these secondary forms
> are fired if I exit the Application by closing the Main Form,
> or calling Application.Terminate;

Do you use Application as owner for these forms when you create them?
If not they will not get destroyed in a regular fashion when the
application closes and the forms are still open.


--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

TObject

unread,
May 5, 2003, 2:30:25 PM5/5/03
to
> Do you use Application as owner for these forms when you create them?

Yes, I do. Here is a fragment of the actual code:

===========================================
{Add New Customer}
NewCustomer:= TBPOCustomer.Create;
try
NewCustomer.IntegerID := BPO_CUST_ID_NEW_CUST;
CustomerForm:= TfrmBPOCustomer.Create(Application);
try
CustomerForm.Customer := NewCustomer;
CustomerForm.FreeCustomerOnClose := True;
CustomerForm.Show;
except
FreeAndNil(CustomerForm);
raise;
end;
except
FreeAndNil(NewCustomer);
raise;
end;
===========================================
This code is called in the application, when operator wants to add
a new customer. They can add as many new customers at the same time
as they want, that is why I use Show, instead of ShowModal.

TfrmBPOCustomer type is for the form that contains entry fields, like
customer name, and the usual stuff. In the OnClose event of the form I set
the Action to acFree. CustomerForm variable is [a pointer to] the object
of this type.

The TBPOCustomer type is the actuall customer class. It does not
descent from TComponent, and does not have Owner capability.

I free the instance of the TfrmBPOCustomer (NewCustomer) in
the CustomerForm's OnDestroy event.

The problem is, OnDestry nor OnClose get fired, when
Application.Terminate happens or the main form closes.

[Confusion mode on]
Just before posting this message I decided to check myself once again.
Yesterday, I was using ShowMessage statements embedded in
the code to determine if certain events get fired or not. Today, I decided
to put some breakpoints and step through with the debugger.

This showed that even though the OnClose event did not get fired,
the OnDestroy event actually did. It is just that the call to the ShowMessage
did not produce a message box. Interesting...
[Confusion mode off]

This is not a really big problem, as I have this code in the application
main form's OnCloseQuery event:

CanClose := MessageDlg('Are you sure you want to exit?'#13#10+
'If you made some changes, and didn''t save them,'#13#10+
'these changes will be lost...',mtConfirmation,[mbYes,mbNo],0)
= mrYes;

But still, I would like to learn on the best way to free my stuff in
the orderly fashion.


Thanks!


TObject

unread,
May 5, 2003, 2:36:03 PM5/5/03
to
> You need to set the Owner of your secondary forms to be either
> application or the main form. This way, before the application or the
> main form gets freed, the code to free your forms will be called.

That's a big 10-4. You are correct. That's how I always do it.

BTW, I create a form, and specify the Application to be the Owner,
and then I usually free the form manually. Do I need to do anything
special to keep the Application from trying to release my form,
I already freed, when it terminates?

Romulo A. Ceccon

unread,
May 5, 2003, 2:56:51 PM5/5/03
to
TObject wrote:

> Do I need to do anything
> special to keep the Application from trying to release my form,
> I already freed, when it terminates?

No. The owner will be notified if one of the owned components is freed.

Romulo

TObject

unread,
May 5, 2003, 3:18:53 PM5/5/03
to
> No. The owner will be notified if one of the owned components is freed.

Got it. The TComponent maintains a list of child objects and handles
Notification messages. When an opRemove notification comes for
a component, that component gets removed from the list.

Thanks for info!

Peter Below (TeamB)

unread,
May 6, 2003, 5:47:18 AM5/6/03
to
In article <3eb6acb0$1...@newsgroups.borland.com>, TObject wrote:
> The problem is, OnDestry nor OnClose get fired, when
> Application.Terminate happens or the main form closes.

OnDestroy should fire, the owned forms will be destroyed when the
application object itself is destroyed. The only situation i know about
when this will not happen in the expected manner is when Windows is
shut down.

As you found ShowMessage is not a good way to debug things, especially
during the shutdown phase. Use OutputDebugString, or
Windows.MessageBox, or write log output to a file.

0 new messages