Board index » cppbuilder » Re: ~mainform vs OnClose OnDestroy, - mainform vs OnCreate, OnActivate..
Alisdair Meredith [TeamB]
Re: ~mainform vs OnClose OnDestroy, - mainform vs OnCreate, OnActivate..
2005-07-29 08:55:01 PM
understand the rule, how do we know when to break it ;?)
I was happliy using the Mainform Events in my program to do various
The problem with the OnCreate and OnDestroy events is their Delphi
legacy. In most ways the languages are compatible, otherwise a C++
binding for VCL would not be possible. However, the way that C++
constructs objects is very different to the way Delphi constructs
objects, and this difference is most noticable in OnCreate/OnDestroy.
The part that causes the problem is:
OnCreate will be called *before* your constructor
OnDestroy will be called *after* your destructor
In theory, this should not be possible in C++ and it is a real problem
if any data members in your form are also of class-type, as their
constructors will not have run either (or their destructors will be
The reason is Delphi starts construction with the most-derived object,
and it is the responsibility of this constructor to call a base-class
constructor. In C++ construction is essentially the other way round,
starting with the most basic type and constructing layer by layer until
you finish constructing the actual type.
This is most noticable when you examine virtual functions. In Delphi,
a virtual function will use dynamic-dispatch to the most derived type,
even during construction/destruction. That is why it must start
constructing at the top - so that the top level class can make sure it
has constructed anything that might be needed during a virtual call.
In C++ the 'type' of your object during construction (for a virtual
function) is the (sub)type that is currently under construction, so you
do not get virtual overides called until after that class has started
construction. This is how C++ guarantees all objects are safely
constructed if a virtual function is called.
C++Builder is wierd. If Delphi construction is 'top-down' and C++ is
'bottom-up', then C++Builder is 'inside-out'. For a C++ class derived
from a VCL class, construction will start somewhere in the middle of
the hierarchy, with the most-derived Delphi class! From here you get
top-down construction, until that class finished constuction. This is
then the constructed base-class that allows C++ construction to finish
in a bottom-up manner.
But at all times, virtual functions are 'live' meaning your C++ code
can be called even though your object and members are not yet
constructed. The same can happen after destruction.
This means the problem is not only with OnCreate and OnDestroy, but
with any virtual method call that can happen when constructing Delphi
base classes (or any that you call yourself during construction) Other
good examples are OnShow and OnActivate, if the form has Visible=true
set in the designer. Note that these events are 'safe' if
Visible=false, and you explicitly call Show(), so the problem is indeed
Also note that this only applies to VCL classes i.e. any class which
ultimately derives from TObject. 'pure' C++ classes are still handled
correctly in BCB [This will not be true for MS new C++/CLI binding for
.NET, where 'deep virtuals' become normal for native C++ classes too]
OK, that is quite a long explaination for a newsgroup posting, yet
barely scratches the surface! Hope it helps you understand this tricky