Hi,
The program behavior with return, exit () [ and abort () ]
is not the same one.
Calling the destructors in a main () program
depends on the termination way : return, exit or abort.
The example is shown below.
Alex
##############################################
############ 1.1 The foo_AAA.H file ##########
##############################################
#include <string>
#include <assert.h>
//----------
class AAA
{
private :
static int count_s;
int number_;
string name_;
public :
AAA ();
AAA (const string& name_i);
~AAA ();
};
##############################################
##############################################
############ 1.2 The foo_AAA.C file ##########
##############################################
#include "foo_AAA.H"
//----------
int AAA::count_s (0);
//----------
// Constructor-0
AAA::AAA ()
{
// Empty
}
//----------
// Constructor-1
AAA::AAA (const string& name_i)
{
name_ = name_i;
number_ = ++count_s;
cout << "Constructor called : Name#"
<< number_
<< " = "
<< name_
<< endl;
}
//----------
// Destructor
AAA::~AAA ()
{
cout << "Destructor called : Name#"
<< number_
<< " = "
<< name_
<< endl;
}
##############################################
##############################################
############ 1.3 The foo_main.C file #########
##############################################
#include "foo_AAA.H"
//----------
AAA obj1 ("global");
//----------
int main (int argc, char ** argv)
{
cout << "\tProgram " << argv [0] << " -> STARTED" << endl;
const string RETURN_termination = "RETURN";
const string EXIT_termination = "EXIT";
const string ABORT_termination = "ABORT";
if (!((argc == 2) &&
((argv [1] == RETURN_termination) ||
(argv [1] == EXIT_termination) ||
(argv [1] == ABORT_termination)
)
))
{
cout << "USAGE : "
<< argv [0]
<< " termination_type"
<< endl;
cout << " --- "
<< RETURN_termination
<< endl;
cout << " --- "
<< EXIT_termination
<< endl;
cout << " --- "
<< ABORT_termination
<< endl;
cout << "\tProgram "
<< argv [0]
<< " -> FATAL TERMINATION"
<< endl;
assert (0);
}
string termination_status = argv [1];
//==========================
AAA obj2 ("local_auto");
static AAA obj3 ("local_static");
//==========================
cout << "\tProgram "
<< argv [0]
<< " -> FINISHED : "
<< termination_status
<< endl;
if (termination_status == RETURN_termination)
{
return 0;
}
if (termination_status == EXIT_termination)
{
exit (0);
}
if (termination_status == ABORT_termination)
{
abort ();
}
assert (0);
} // int main (int argc, char ** argv)
##############################################
##############################################
############ 2.1 a.out RETURN ################
######## The results of the running ##########
##############################################
Constructor called : Name#1 = global
Program a.out -> STARTED
Constructor called : Name#2 = local_auto
Constructor called : Name#3 = local_static
Program a.out -> FINISHED : RETURN
Destructor called : Name#2 = local_auto
Destructor called : Name#3 = local_static
Destructor called : Name#1 = global
//======== Conclusion =======
// All destructors are called
//===========================
##############################################
############ 2.2 a.out EXIT ##################
######## The results of the running ##########
##############################################
Constructor called : Name#1 = global
Program a.out -> STARTED
Constructor called : Name#2 = local_auto
Constructor called : Name#3 = local_static
Program a.out -> FINISHED : EXIT
Destructor called : Name#3 = local_static
Destructor called : Name#1 = global
//======== Conclusion =======
// The local_auto object destructor isn't called
//===========================
##############################################
############ 2.3 a.out ABORT #################
######## The results of the running ##########
##############################################
Constructor called : Name#1 = global
Program a.out -> STARTED
Constructor called : Name#2 = local_auto
Constructor called : Name#3 = local_static
Program a.out -> FINISHED : ABORT
//======== Conclusion =======
// No destructor is called
//===========================
##############################################
############ 3. Compiler #####################
##############################################
g++ -v : gcc version egcs-2.91.57 19980901 (egcs-1.1 release)
##############################################
############ 4. System #######################
##############################################
uname -a : SunOS <nodename> 5.6 Generic_105181-09
sun4m sparc SUNW,SPARCstation-5
##############################################
############ END OF INFORMATION ##############
##############################################
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own