Exception Handling Question and Unwinding stack

Discussion in 'C++' started by DanielJohnson, Feb 11, 2008.

  1. I have a little confusion with this kind of situation.

    Suppose there are two functions, Function First allocates a memory and
    then call Function Second..... But in Function Second Exception
    occurs!!! How can we notify Function First about this in order to
    deallocate the memory allocated by First ?

    I found something from Stroutup's web page, saying that it can't be
    done ?

    http://www.research.att.com/~bs/bs_faq2.html#resume

    I was thinking that if we can catch this exception in the second
    function then either 1) rethrow so that the first function can catch
    it then deallocate appropriately 2) Catch and return an error value
    OR
    The second is that a function doesn't have to catch it at all. The
    exception would automatically be rethrow to the first function.


    Can C++ gurus throw some insight into such situations ?
     
    DanielJohnson, Feb 11, 2008
    #1
    1. Advertising

  2. * DanielJohnson:
    > I have a little confusion with this kind of situation.
    >
    > Suppose there are two functions, Function First allocates a memory and
    > then call Function Second..... But in Function Second Exception
    > occurs!!! How can we notify Function First about this in order to
    > deallocate the memory allocated by First ?
    >
    > I found something from Stroutup's web page, saying that it can't be
    > done ?
    >
    > http://www.research.att.com/~bs/bs_faq2.html#resume
    >
    > I was thinking that if we can catch this exception in the second
    > function then either 1) rethrow so that the first function can catch
    > it then deallocate appropriately 2) Catch and return an error value
    > OR
    > The second is that a function doesn't have to catch it at all. The
    > exception would automatically be rethrow to the first function.
    >
    >
    > Can C++ gurus throw some insight into such situations ?


    It seems you have misunderstood Bjarne's comment, which was about
    resumption.

    Ordinary C++ exception handling suffices for the situation you describe.

    In particular,

    void foo()
    {
    std::vector<int> v(256);
    bar();
    tender();
    }

    allocates memory dynamically for v and, if that succeeds, is guaranteed
    to deallocate that memory if bar() or tender() throws.

    Resumption, to continue execution at the point of the throw, is fraught
    with dangers. However, in some cases you may want a function or block
    to retry, perhaps in some other way, if an exception occurs. The basic
    mechanism is then a loop, and that retry loop may be abstracted by using
    the template pattern (which has nothing to do with C++ templates).


    Cheers, & hth.,

    - Alf

    --
    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?
     
    Alf P. Steinbach, Feb 11, 2008
    #2
    1. Advertising

  3. On 2008-02-11 19:12, DanielJohnson wrote:
    > I have a little confusion with this kind of situation.
    >
    > Suppose there are two functions, Function First allocates a memory and
    > then call Function Second..... But in Function Second Exception
    > occurs!!! How can we notify Function First about this in order to
    > deallocate the memory allocated by First ?
    >
    > I found something from Stroutup's web page, saying that it can't be
    > done ?
    >
    > http://www.research.att.com/~bs/bs_faq2.html#resume
    >
    > I was thinking that if we can catch this exception in the second
    > function then either 1) rethrow so that the first function can catch
    > it then deallocate appropriately 2) Catch and return an error value
    > OR
    > The second is that a function doesn't have to catch it at all. The
    > exception would automatically be rethrow to the first function.


    You are misinterpreting the link. There are three ways to notify First
    that an exception occurred: 1) you can just let the exception propagate
    and catch it in First, 2) you can catch the exception in Second and
    throw a new/rethrow, or 3) you can catch in Second and return with some
    error code.

    What the link says is that you can not in either First or Second perform
    some kind of error recovery and then re-run the code that threw the
    exception (though you can of course call Second again from First,
    perhaps in a loop)

    --
    Erik Wikström
     
    Erik Wikström, Feb 11, 2008
    #3
  4. DanielJohnson

    Martin York Guest

    > How can we notify Function First about this in order to
    > deallocate the memory allocated by First ?



    C++ handles resource management via the constructor/destructor
    mechanism. Just make sure all memory allocation/deallocation is done
    inside an objects construtor/destructor. Then create the object on the
    stack. When the stack unwinds the destructor will auto-magically de-
    allocate the resource.

    std::vector<> is great if you want to allocate a bunch of objects
    dynamically.
    std::auto_ptr<> is find to allocate one object.

    But if you must do it yourself then somthing like this:

    class MyMemoryForX
    {
    MyMemoryForX(int size)
    {
    data = new X[size];
    }
    ~MyMemoryForX()
    {
    delete [] data;
    }
    private:
    X* data;

    MyMemoryForX(MyMemoryForX const&); // don't define
    void operator=(MyMemoryForX const&); // don't define
    };

    void first
    {
    MyMemoryForX x(200);
    second(); // Any exception thrown in here will cause
    // the stack to un-wind and the destructor
    // for x to be called. Thus resulting in
    // resource clean up.
    }
     
    Martin York, Feb 11, 2008
    #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. Senthilvel

    Stack unwinding

    Senthilvel, Jul 5, 2004, in forum: C++
    Replies:
    5
    Views:
    3,740
    Senthilvel
    Jul 5, 2004
  2. Steven T. Hatton

    Stack unwinding and destructors?

    Steven T. Hatton, Aug 9, 2004, in forum: C++
    Replies:
    2
    Views:
    553
    John Harrison
    Aug 9, 2004
  3. Tony
    Replies:
    4
    Views:
    481
  4. George2

    Stack unwinding

    George2, Jan 23, 2008, in forum: C++
    Replies:
    2
    Views:
    364
    James Kanze
    Jan 24, 2008
  5. Jim King
    Replies:
    7
    Views:
    930
    Jeff Flinn
    Nov 2, 2010
Loading...

Share This Page