Discussion:
Form destructor - two questions
(too old to reply)
Erik Doekes
2008-07-04 18:12:51 UTC
Permalink
Hi,

if messages from the ShowMessage routine are not shown although they should
be executed during the form's destructor, does this mean that something is
wrong? Or is it just too late too show messages when the form's destructor
is called?

I have a struct FileInfo whose destructor shows the message "Deleted", and
my main form contains a TList with FileInfo objects. In the form's
destructor any remaining objects in the list are deleted:
for(int i = 0; i < Files->Count; ++i)
delete (FileInfo*) Files->Items[i];
But when I close my program, still having FileInfo objects left, I don't get
to see this message
(which I do get when I delete the objects elsewhere in the program using the
same syntax).

I also tested to place the line ShowMessage("Test"); directly in the form's
destructor, and that one was not executed either.
Does this mean that everything is as it should be?

Question two: is it not recommendable to write code in the constructor and
in the onDestroy event?
(Instead of constructor+destructor / onCreate+onDestroy)
I could not find the practical difference between using the destructor and
using onDestroy in the help file, but it did recommend to use onDestroy for
deleting objects created in the form's onCreate event.

Regards,
Erik Berger
Clayton Arends
2008-07-04 18:28:14 UTC
Permalink
Post by Erik Doekes
if messages from the ShowMessage routine are not shown although they
should be executed during the form's destructor, does this mean that
something is wrong? Or is it just too late too show messages when the
form's destructor is called?
If the form being destroyed is your main form then the message will not be
shown.
Post by Erik Doekes
Does this mean that everything is as it should be?
Yes. If you need to show messages when the form is closing then add an
OnClose handler for the form.
Post by Erik Doekes
Question two: is it not recommendable to write code in the constructor
and in the onDestroy event?
(Instead of constructor+destructor / onCreate+onDestroy)
Stay away from using OnCreate and OnDestroy in C++Builder applications.
They work fine in Delphi applications but in C++ use the constructor and
destructor instead. This particular question has been asked and answered
many times. To learn more about it do a newsgroup search using
http://groups.google.com or other similar search engine.

Clayton
Remy Lebeau (TeamB)
2008-07-06 07:03:41 UTC
Permalink
Post by Erik Doekes
if messages from the ShowMessage routine are not shown although
they should be executed during the form's destructor, does this mean
that something is wrong? Or is it just too late too show messages
when the form's destructor is called?
It is too late to call ShowMessage() specifically, but not to display
messages in general. Internally, ShowMessage() displays a modal TForm.
However, the TForm::ShowModal() method looks at the TApplication::Terminated
property, which is set to true by the time the MainForm destructor is
called. So ShowMessage() exits immediately without giving you a chance to
see the dialog box. If you use the Win32 API MessageBox() function instead,
you will see a dialog appear as expected.
Post by Erik Doekes
is it not recommendable to write code in the constructor and in the
onDestroy event?
Do use the destructor. Do not use the OnDestroy event.
Post by Erik Doekes
I could not find the practical difference between using the destructor
and using onDestroy in the help file
They are called at different times during the form's destruction.
Post by Erik Doekes
but it did recommend to use onDestroy for deleting objects
created in the form's onCreate event.
The help file is heavily Delphi-oriented. OnCreate/OnDestroy work fine in
Delphi. They are dangerous to use in C++. Use the constructor/destructor
instead.


Gambit

Loading...