Exception Destructor on new

S

Subra

Hi,

I am learning C++ and need export advice on the below program.
I have read it that whenever a exception is genrated and control
enteres the catch block it will call destructors for all the
successfuly completed constructors. But in the below case
i am doing :-
base *ptr=new base;
throw 44;
But the destructor for base is not called once it enters the catch
block. Please let me know why it is so and how to come out of it.

#include<iostream>
#include<exception>
using namespace std;
class base
{
public:
base() { cout<<"Constructor"<<endl; }
~base() { cout<<"Destructor"<<endl; }
};
int main()
{
try {
base *ptr=new base;
throw 44;
}catch(...) { // I am
expecting base destructor to be called here
cout<<" Inside catch"<<endl; // But it is not
}
}
 
A

Alf P. Steinbach

* Subra:
Hi,

I am learning C++ and need export advice on the below program.
I have read it that whenever a exception is genrated and control
enteres the catch block it will call destructors for all the
successfuly completed constructors. But in the below case
i am doing :-
base *ptr=new base;
throw 44;
But the destructor for base is not called once it enters the catch
block. Please let me know why it is so and how to come out of it.

The pointer is "destroyed", in a sense.

The object it points to is not.

To have the pointed to object destroyed along with the pointer, use a
smart pointer such as std::auto_ptr.


#include<iostream>
#include<exception>

You don't need the <exception> header, you're not using anything from
that header.

To use std::auto_ptr you need

using namespace std;
class base
{
public:
base() { cout<<"Constructor"<<endl; }
~base() { cout<<"Destructor"<<endl; }
};
int main()
{
try {
base *ptr=new base;

Make that

throw 44;
}catch(...) { // I am
expecting base destructor to be called here
cout<<" Inside catch"<<endl; // But it is not
}
}

Hth.,

- Alf (an export on C++)
 
S

Sarath

Another approach is

int main()
{
base *ptr = NULL;
try
{
ptr=new base;
throw 44;
}
catch(...)
{
if(p)
delete ptr;
cout<<" Inside catch"<<endl;
}
 
M

Marcus Kwok

Subra said:
Hi,

I am learning C++ and need export advice on the below program.
I have read it that whenever a exception is genrated and control
enteres the catch block it will call destructors for all the
successfuly completed constructors. But in the below case
i am doing :-
base *ptr=new base;
throw 44;
But the destructor for base is not called once it enters the catch
block. Please let me know why it is so and how to come out of it.

#include<iostream>
#include<exception>
using namespace std;
class base
{
public:
base() { cout<<"Constructor"<<endl; }
~base() { cout<<"Destructor"<<endl; }
};
int main()
{
try {
base *ptr=new base;
throw 44;
}catch(...) { // I am
expecting base destructor to be called here
cout<<" Inside catch"<<endl; // But it is not
}
}

Alf Steinbach has given you one suggestion. However, if you do not need
to allocate base dynamically, then observe this program:

#include <iostream>

class Base {
public:
Base() { std::cout << "Constructor\n"; }
~Base() { std::cout << "Destructor\n"; }
};

int main()
{
try {
Base b;
throw 44;
}
catch (...) {
std::cout << "Inside catch\n";
}
}
 
J

Jim Langston

Subra said:
Hi,

I am learning C++ and need export advice on the below program.
I have read it that whenever a exception is genrated and control
enteres the catch block it will call destructors for all the
successfuly completed constructors. But in the below case
i am doing :-
base *ptr=new base;
throw 44;
But the destructor for base is not called once it enters the catch
block. Please let me know why it is so and how to come out of it.

#include<iostream>
#include<exception>
using namespace std;
class base
{
public:
base() { cout<<"Constructor"<<endl; }
~base() { cout<<"Destructor"<<endl; }
};
int main()
{
try {
base *ptr=new base;

You are constructing a base pointer. With new it calls a base constructor.
throw 44;

Even without this throw, the destructor for base isn't going to be called,
because you never did delete ptr.
}catch(...) { // I am
expecting base destructor to be called here
cout<<" Inside catch"<<endl; // But it is not
}

The base pointer is destroyed. But, if you call new, you have to call
delete. Which you haven't.

Objects instantized with new never have their destructors called unless you
call delete (there may be exceptions, but this is the general rule). If you
want your base instance destructor called (and you do) then you have to call
delete ptr.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,261
Messages
2,571,040
Members
48,769
Latest member
Clifft

Latest Threads

Top