By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Exception Destructor on new

P: n/a
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
}
}

Mar 23 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* 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 <exceptionheader, you're not using anything from
that header.

To use std::auto_ptr you need

#include <memory>

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

std::auto_ptr<baseptr( new base );

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++)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 23 '07 #2

P: n/a
Another approach is

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

Mar 23 '07 #3

P: n/a
Subra <ma*********@gmail.comwrote:
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";
}
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 23 '07 #4

P: n/a
"Subra" <ma*********@gmail.comwrote in message
news:11**********************@n76g2000hsh.googlegr oups.com...
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.
}

Mar 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.