473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Deallocation upon constructor failure

Hello all,

In the program below, why am I not seeing operator delete being invoked? If
a constructor throws, isn't allocated memory supposed to be released and
then the exception re-propagated?

Thanks,
Dave
#include <iostream>

using namespace std;

class foo
{
public:
foo() { throw 1; }

static void *operator new(size_t s)
{
cout << "foo::operator new()" << endl;
return ::operator new(s);
}

static void operator delete(void *p)
{
cout << "foo::operator delete()" << endl;
::operator delete(p);
}
};

int main()
{
new foo;
}

Aug 11 '05 #1
4 2577
* Dave:

In the program below, why am I not seeing operator delete being invoked?


Because execution goes straight to std::terminate.

Catch that exception somewhere.

--
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?
Aug 11 '05 #2
> > In the program below, why am I not seeing operator delete being invoked?

Because execution goes straight to std::terminate.

Catch that exception somewhere.


Here's the program I'm now running per your suggestion:

#include <iostream>

using namespace std;

class foo
{
public:
foo() { throw 1; }

static void *operator new(size_t s)
{
cout << "foo::operator new()" << endl;
return ::operator new(s);
}

static void operator delete(void *p)
{
cout << "foo::operator delete()" << endl;
::operator delete(p);
}
};

int main()
{
try
{
new foo;
}
catch(...)
{
cout << "Exception handler" << endl;
}
}

OK, catching the exception in main() did allow foo::operator delete() to be
called. Here's the output:

foo::operator new()
foo::operator delete()
Exception handler

So, operator delete is called immediately after operator new().
Specifically, it is called before the exception is propagated on. Now my
question is: Why did I have to catch the exception in order for operator
delete to be called given that the call to operator delete() occurs before
any exception processing anyway? What is the real sequence of events going
on here, both with and without the exception handler?

Thanks!
Aug 11 '05 #3
* Dave:

Now my question is: Why did I have to catch the exception in order for
operator delete to be called
As I recall, whether cleanup is performed or not when you have an unhandled
exception, is implementation defined.

given that the call to operator delete() occurs before
any exception processing anyway?
That statement, "anyway", is contrary to the facts you have established.

What is the real sequence of events going
on here, both with and without the exception handler?


With: operator delete, catch handler, normal exit.
Without: std::terminate, bye.

--
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?
Aug 11 '05 #4

"Dave" <be***********@yahoo.com> wrote in message
news:11*************@news.supernews.com...
In the program below, why am I not seeing operator delete being
invoked?
Because execution goes straight to std::terminate.

Catch that exception somewhere.
Here's the program I'm now running per your suggestion:

#include <iostream>

using namespace std;

class foo
{
public:
foo() { throw 1; }

static void *operator new(size_t s)
{
cout << "foo::operator new()" << endl;
return ::operator new(s);
}

static void operator delete(void *p)
{
cout << "foo::operator delete()" << endl;
::operator delete(p);
}
};

int main()
{
try
{
new foo;
}
catch(...)
{
cout << "Exception handler" << endl;
}
}

OK, catching the exception in main() did allow foo::operator delete() to

be called. Here's the output:

foo::operator new()
foo::operator delete()
Exception handler

So, operator delete is called immediately after operator new().
Specifically, it is called before the exception is propagated on. Now my
question is: Why did I have to catch the exception in order for operator
delete to be called given that the call to operator delete() occurs before
any exception processing anyway? What is the real sequence of events going
on here, both with and without the exception handler?

Thanks!


As it turns out, I believe I have a non-compliant compiler.5.3.4/17
indicates my deallocation function should be called regardless of whether or
not the exception is caught.
Aug 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Boogie El Aceitoso | last post by:
Hi, I have a class whose constructor accepts a filename and performs some actions on it. Some things might go wrong, such as not being the right kind of file, the file doesn't exist, is...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
12
by: BigBrian | last post by:
I realize this isn't the best code, but I'm doing maintenance on a legcy system that has something similar to the following... class Foo { //... } int main() {
4
by: the friendly display name | last post by:
Hello, Well, the problem is clear: How to call a constructor of a programmaticaly loaded user control? The LoadControl function obviously won't do it. And using this: <%@ Register...
1
by: yancheng.cheok | last post by:
Hi all, According to "How can I handle a constructor that fails?" in http://www.parashift.com/c++-faq-lite/exceptions.html#faq-17.2, whenever there is a constructor fail, we will throw...
40
by: Sek | last post by:
Is it appropriate to throw exception from a constructor? Thats the only way i could think of to denote the failure of constructor, wherein i am invoking couple of other classes to initialise the...
19
by: pasa_1 | last post by:
Do you see anything wrong with using constructor 3 followed by a function foo() as I have defined below? class A { member_variable_1; ptr_member_variable_2; public: // Constructor 1 A():
0
by: divyabarathii | last post by:
hi i declared a structure as shown below Struct Name { int *nPtr; float *fPtr; } In MFC, one command button click event, i added the following code and CDocClass has the structure as...
18
by: welch.ryan | last post by:
Hi all, Having a problem with addressing large amounts of memory. I have a simple piece of code here that is meant to allocate a large piece of memory on a ppc64 machine. The code is: /*...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.