Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > new and throw in c'tor

Reply
Thread Tools

new and throw in c'tor

 
 
Chameleon
Guest
Posts: n/a
 
      01-10-2007
--------------------------------
class A {
int data[1000];
public:
A() { throw; }
};

int main(int, char**) {
try {
A *a = new A;
} catch(...) {}
}
--------------------------------

In code above we have a class which throws in c'tor. Object *a never
created, so d'tor of A never runs.

sizeof(A) is 1000 * sizeof(int), so with new, a block of (not always)
4000 bytes allocated in memory but pointer to that block, never stored
in 'a'. So 'delete a;' is not working.

The question is:
We have a memory leak here?
If yes, how we can fix it?

Thanks
 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      01-10-2007
Chameleon wrote:
> --------------------------------
> class A {
> int data[1000];
> public:
> A() { throw; }
> };
>
> int main(int, char**) {
> try {
> A *a = new A;
> } catch(...) {}
> }
> --------------------------------
>
> In code above we have a class which throws in c'tor. Object *a never
> created, so d'tor of A never runs.
>
> sizeof(A) is 1000 * sizeof(int), so with new, a block of (not always)
> 4000 bytes allocated in memory but pointer to that block, never stored
> in 'a'. So 'delete a;' is not working.
>
> The question is:
> We have a memory leak here?
> If yes, how we can fix it?
>
> Thanks


There's no leak. See this FAQ for what happens behind the scenes when
you use the new operator (search for "functionally"):

http://www.parashift.com/c++-faq-lit...html#faq-11.14

Cheers! --M

 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      01-10-2007
Chameleon wrote:
> --------------------------------
> class A {
> int data[1000];
> public:
> A() { throw; }
> };
>
> int main(int, char**) {
> try {
> A *a = new A;
> } catch(...) {}
> }
> --------------------------------
>
> In code above we have a class which throws in c'tor. Object *a never
> created, so d'tor of A never runs.


In the code above you end up in terminate because you are doing a
rethrow outside of the context of a handled exception. You want to
throw an object:
throw 0;

>
> sizeof(A) is 1000 * sizeof(int), so with new, a block of (not always)
> 4000 bytes allocated in memory but pointer to that block, never stored
> in 'a'. So 'delete a;' is not working.
>
> The question is:
> We have a memory leak here?
> If yes, how we can fix it?
>

No you don't have a memory leak here (provided you fix the screwup
above). If an exception occurs during new, the memory is returned
via the deallocation function. The only thing you must be careful
of is that the constructor must clean up after itself:

class A {
int* foo;
public:
A() {
foo = new int[1000];
throw 0;
}
};

Here it the allocation stored in foo is leaked .
 
Reply With Quote
 
Peter
Guest
Posts: n/a
 
      01-10-2007

Chameleon wrote:
> --------------------------------
> class A {
> int data[1000];
> public:
> A() { throw; }
> };
>
> int main(int, char**) {
> try {
> A *a = new A;
> } catch(...) {}
> }
> --------------------------------
>
> In code above we have a class which throws in c'tor. Object *a never
> created, so d'tor of A never runs.
>
> sizeof(A) is 1000 * sizeof(int), so with new, a block of (not always)
> 4000 bytes allocated in memory but pointer to that block, never stored
> in 'a'. So 'delete a;' is not working.
>
> The question is:
> We have a memory leak here?
> If yes, how we can fix it?



Welcome to the world of C++-Exception-Handling.

I'm curious that this question does not come up more often.
I guess most programmers ignore C++ Exception handling.
It is the most underused feature of C++.
But you gain incredible code savings if you do not have to check for
success anymore.
Also it is the only way to abort the construction of an object.
This way you can do your fallible allocation where it belongs -- inside
the constructor.
Also it enables very rich error reporting.
Consider catching and rethrowing an new exception object containing a
copy of the caught exception object. This way you can easily create
code, which gives useful rich error messages, like:

Cannot compile file, because of
Cannot preprocess file, because of
Cannot create temporary file, because of
Disk Full!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To throw or to throw not? Emanuele D'Arrigo Python 6 11-15-2008 04:12 PM
All New IIS virtual directories with ASP.NET throw exception "initial character not valid" Pao ASP .Net 12 04-04-2007 05:13 PM
JNI's throw new does not throw an exception yarona@m-sys.com Java 15 09-08-2005 08:36 AM
Cannot display throw new exception error message Matt Java 2 07-01-2004 07:07 AM
Throw Exception Vs Throw New Exception Kerri ASP .Net 2 10-27-2003 02:13 PM



Advertisments