is such exception handling approach good?

Discussion in 'C++' started by George2, Dec 21, 2007.

  1. George2

    George2 Guest

    Hello everyone,


    Suppose I have some objects created on local function stack (not on
    heap). And I allocate and free all the related resources in the
    constructor and destructor (e.g. memory and file handles). And I do
    not implement explicit exception handling code in the function for
    resource free purpose, and simply rely on destructor to free resources
    if exception occurs, that is,

    1. when exception occurs and the exception triggers us to go out of
    current function stack to the caller to find exception handlers;
    2. since objects are allocated on local stack, when exception triggers
    us to go out of current stack to its caller to find exception handler,
    the lifecycle of local objects on local stack will expire, and its
    related destructor will be invoked and free resources.

    Pseudo code like this,

    Code:
    void func()
    {
          Class1 inst1;
          Class2 inst2;
    
          ... // some operations
    
          return;
    }
    
    Does above code always safe? Are there any potential risks to leak
    resources?


    thanks in advance,
    George
    George2, Dec 21, 2007
    #1
    1. Advertising

  2. George2

    Rolf Magnus Guest

    George2 wrote:

    > Hello everyone,
    >
    >
    > Suppose I have some objects created on local function stack (not on
    > heap).


    The C++ standard does define the words "heap" and "stack", but both mean
    something different from what you are talking about.
    Local variables go to automatic storage. Dynamic objects go to the free
    storage.

    > 1. when exception occurs and the exception triggers us to go out of
    > current function stack to the caller to find exception handlers;
    > 2. since objects are allocated on local stack, when exception triggers
    > us to go out of current stack to its caller to find exception handler,
    > the lifecycle of local objects on local stack will expire, and its
    > related destructor will be invoked and free resources.


    Yes.

    >
    > Pseudo code like this,
    >
    >
    Code:
    > void func()
    > {
    >       Class1 inst1;
    >       Class2 inst2;
    > 
    >       ... // some operations
    > 
    >       return;
    > }
    > 
    >
    > Does above code always safe? Are there any potential risks to leak
    > resources?


    If Class1 and Class2 have proper destructors that release all acquired
    resources, no. That is the RAII principle.
    Rolf Magnus, Dec 21, 2007
    #2
    1. Advertising

  3. George2

    Sachin Guest

    On Dec 21, 5:44 pm, George2 <> wrote:
    > Hello everyone,
    >
    > Suppose I have some objects created on local function stack (not on
    > heap). And I allocate and free all the related resources in the
    > constructor and destructor (e.g. memory and file handles). And I do
    > not implement explicit exception handling code in the function for
    > resource free purpose, and simply rely on destructor to free resources
    > if exception occurs, that is,
    >
    > 1. when exception occurs and the exception triggers us to go out of
    > current function stack to the caller to find exception handlers;
    > 2. since objects are allocated on local stack, when exception triggers
    > us to go out of current stack to its caller to find exception handler,
    > the lifecycle of local objects on local stack will expire, and its
    > related destructor will be invoked and free resources.
    >
    > Pseudo code like this,
    >
    >
    Code:
    > void func()
    > {
    >       Class1 inst1;
    >       Class2 inst2;
    >
    >       ... // some operations
    >
    >       return;}
    >
    > 
    >
    > Does above code always safe? Are there any potential risks to leak
    > resources?
    >
    > thanks in advance,
    > George


    If we encounter any exception within function destructor will not be
    invoked, and there are high probability of leakage I bealieve.

    Thanks,
    Sachin
    Sachin, Dec 24, 2007
    #3
  4. George2

    Rolf Magnus Guest

    Sachin wrote:

    > On Dec 21, 5:44 pm, George2 <> wrote:
    >> Hello everyone,
    >>
    >> Suppose I have some objects created on local function stack (not on
    >> heap). And I allocate and free all the related resources in the
    >> constructor and destructor (e.g. memory and file handles). And I do
    >> not implement explicit exception handling code in the function for
    >> resource free purpose, and simply rely on destructor to free resources
    >> if exception occurs, that is,
    >>
    >> 1. when exception occurs and the exception triggers us to go out of
    >> current function stack to the caller to find exception handlers;
    >> 2. since objects are allocated on local stack, when exception triggers
    >> us to go out of current stack to its caller to find exception handler,
    >> the lifecycle of local objects on local stack will expire, and its
    >> related destructor will be invoked and free resources.
    >>
    >> Pseudo code like this,
    >>
    >>
    Code:
    >> void func()
    >> {
    >> Class1 inst1;
    >> Class2 inst2;
    >>
    >> ... // some operations
    >>
    >> return;}
    >>
    >> 
    >>
    >> Does above code always safe? Are there any potential risks to leak
    >> resources?
    >>
    >> thanks in advance,
    >> George

    >
    > If we encounter any exception within function destructor will not be
    > invoked, and there are high probability of leakage I bealieve.


    Your beliefs are wrong. If an exception is thrown, all local objects are
    properly destroyed.
    Rolf Magnus, Dec 24, 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. Jim Bancroft

    Good approach to calling Urls remotely?

    Jim Bancroft, Aug 15, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    344
    =?Utf-8?B?YmlsbHI=?=
    Aug 16, 2005
  2. Peter
    Replies:
    34
    Views:
    1,904
    James Kanze
    Oct 17, 2009
  3. VSK
    Replies:
    0
    Views:
    235
  4. Hernán Castelo

    security : a good approach ?

    Hernán Castelo, Jun 8, 2004, in forum: ASP General
    Replies:
    7
    Views:
    132
    Egbert Nierop \(MVP for IIS\)
    Jun 11, 2004
  5. Telemach
    Replies:
    8
    Views:
    99
    S P Arif Sahari Wibowo
    Feb 19, 2008
Loading...

Share This Page