new and throw in c'tor

Discussion in 'C++' started by Chameleon, Jan 10, 2007.

  1. Chameleon

    Chameleon Guest

    --------------------------------
    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
     
    Chameleon, Jan 10, 2007
    #1
    1. Advertising

  2. Chameleon

    mlimber Guest

    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-lite/dtors.html#faq-11.14

    Cheers! --M
     
    mlimber, Jan 10, 2007
    #2
    1. Advertising

  3. Chameleon

    Ron Natalie Guest

    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 .
     
    Ron Natalie, Jan 10, 2007
    #3
  4. Chameleon

    Peter Guest

    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!
     
    Peter, Jan 10, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kerri
    Replies:
    2
    Views:
    13,065
    Kevin Spencer
    Oct 27, 2003
  2. Raffael Vogler
    Replies:
    8
    Views:
    1,202
    Chris Uppal
    Nov 30, 2003
  3. Replies:
    15
    Views:
    7,640
    Roedy Green
    Sep 8, 2005
  4. Chameleon

    throw from c'tor

    Chameleon, Nov 12, 2006, in forum: C++
    Replies:
    24
    Views:
    747
  5. Emanuele D'Arrigo

    To throw or to throw not?

    Emanuele D'Arrigo, Nov 14, 2008, in forum: Python
    Replies:
    6
    Views:
    338
    Emanuele D'Arrigo
    Nov 15, 2008
Loading...

Share This Page